1

I'm trying to create RESTful service by using ASP.NET Web API 2. I've got a problem when there are two PUT method in my controller.

This is my model:

Subscription
- SubscriptionID (PK - Not Null)
- UserID (FK - Not Null)
- ReportGroupID (FK - Null)
- ReportID (FK - Null)
- IsSubscribe (Not Null)
- SubscribeDate (Not Null)
- UnsubscribeDate (Null)

User can subscribe per report group or per report, so I add another PUT method in Subscription controller. I can add a route as suggested in this link, but it will become RPC-style. Furthermore, it's been 4 years ago and use ASP.NET Web API.

I'm looking for solution in ASP.NET Web API 2 with RESTful-style rather than RPC, is there a way to achieve this?

UPDATE Config code:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Community
  • 1
  • 1
Willy
  • 1,689
  • 7
  • 36
  • 79
  • You can decorate your method with HttpPut attribute, look at http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 and search for HTTP Methods. Or by web api's convention, define your methods as Putxyz where xyz is your method name. Is this what you need? – KKS Sep 28 '16 at 09:52
  • post your config code. a route config like `api/{controller}/{action}/{id}` should do the work. – Amit Kumar Ghosh Sep 28 '16 at 10:38
  • @AmitKumarGhosh I'm using default config. By adding that configuration it works but as defined in http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api. "This style of routing is similar to ASP.NET MVC, and may be appropriate for an RPC-style API." I want to use RESTful-style rather than RPC, is that possible? – Willy Sep 28 '16 at 10:47
  • @SiddharthPandey I try to use Attribute Routing, `[Route("api/Subscription/PutSubscriberByReportGroup")]` and `[Route("api/Subscription/PutSubscriberByReport")]` in each method but the result is 404 the resources not found – Willy Sep 28 '16 at 10:51
  • 1
    By default, only one update operation can exist. – Amit Kumar Ghosh Sep 28 '16 at 10:58

0 Answers0