WEB API routing actions multiple - asp.net-web-api

May I define a Route for an specific Controller ?
The 3th Route, route to a Controller with somes Actions, but the Route only work fine if is placed before the rest.
Some suggestions , please?
// ex: api/groups/Collective1/john01
config.Routes.MapHttpRoute(
name: ControllerAndCollectiveAndUserID,
routeTemplate: "api/{controller}/{collective}/{userid}",
defaults: null, //defaults: new { id = RouteParameter.Optional }
constraints: new
{
collective = #"^[a-zA-Z\d]+$",
userid = #"^[a-zA-Z\d]+$"
}
);
// ex: api/groups/Collective1
config.Routes.MapHttpRoute(
name: ControllerAndCollective,
routeTemplate: "api/{controller}/{collective}",
defaults: null, //defaults: new { id = RouteParameter.Optional }
constraints: new
{
collective = #"^[a-zA-Z\d]+$"
}
);
// ex: api/users/pending
config.Routes.MapHttpRoute(
name: ControllerAction,
routeTemplate: "api/{controller}/{action}"
);
// ex: api/persons
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: ControllerOnly,
routeTemplate: "api/{controller}"
);

Related

WebAPI Multi Route

I added 2 route, the following is my code
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "HandheldApi",
routeTemplate: "api/handheld/{controller}/{action}"
);
When I use the following link http://localhost:64886/api/Account/Get, this link is work for me
But I use the following link http://localhost:64886/api/handheld/Account/Get, the server return "No HTTP resource was found that matches the request URI"
How can I do? I must set 2 route because the new route is new mapping, the default route will be obsoleted
You didn't define the defaults parameter for your second route. Hope this will work-
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "HandheldApi",
routeTemplate: "api/handheld/{controller}/{action}"
defaults: new { controller = "MyController", action = "MyMethod" }
);

something wrong with wep api route

My registered routes mapping looks like this:
config.Routes.MapHttpRoute(
name: "WithActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
And, controller looks like this:
// [Route("???????????")]
[HttpGet]
public HttpResponseMessage GetPatientProviderData([FromUri] PatientProviderIncomingDto ppDto)
{
var response = Request.CreateResponse(HttpStatusCode.OK, _claimDetailService.GetPatientProviderData(ppDto.patientId, ppDto.facilityGroupId, ppDto.claimId));
return response;
}
I'm making a GET call like this:
http://localhost:xxxx/api/claims/GetPatientProviderData?patientId=180&facilityGroupId=9&claimId=21
But, it's not hitting the URL. I was expecting it to hit the URL and that the supplied parameters get converted into DTO.

Custom routing for GET/POST/DELETE does not match

What I'm trying to do is to use similar route template in my custom route as the default route template uses, but getting 405 - METHOD NOT ALLOWED.
First one matches the GET request # api/accounts/abc123/contacts but that's about it. Other two don't match, where as the default route # api/{controller}/{id} matches all four verbs.
EDIT: Updated route definitions
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ContactsApi",
routeTemplate: "api/{controller}/{id1}/contacts"
);
config.Routes.MapHttpRoute(
name: "AddressesApi",
routeTemplate: "api/{controller}/{id2}/addresses"
);
config.Routes.MapHttpRoute(
name: "CoverageApi",
routeTemplate: "api/{controller}/{id3}/coverage"
);
config.Routes.MapHttpRoute(
name: "AccountsApi",
routeTemplate: "api/{controller}/{id4}/accounts"
);
I'm trying to map above routes to below actions:
EDIT: Updated with {id1} parameter as per above route definition.
[HttpGet]
public List<Contact> GetContacts(string id)
{
return accounts.GetContacts(id);
}
[HttpPost]
public void PostContacts(string id1, [FromBody]IEnumerable<Contact> contacts)
{
bool success = accounts.AssignContacts(id, contacts);
}
[HttpDelete]
public void DeleteContacts(string id1, [FromBody]IEnumerable<Contact> contacts)
{
bool success = accounts.RemoveContacts(id, contacts);
}
I just want to keep my routes consistent...
You only need to have the one route to match the actions given above:
config.Routes.MapHttpRoute(
name: "ApiContacts",
routeTemplate: "api/{controller}/{id}/contacts"
);
Because your actions start with the relevant http verb, the corresponding action will be called.

Asp.Net Mvc Web Api Routing 404

I try to something on Web Api. First I will share my WebApiConfig
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = #"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiCategory",
routeTemplate: "api/tales/{category}/{id}",
defaults: new { controller = "Tales", id = RouteParameter.Optional},
constraints: new { category = #"^[a-z]+$" }
);
I read this issue and I fix my apiconfig file. My purpose like this:
List GetAllTales() = api/tales/ -> 404 Not Found
Tale GetTale(int id) = api/tales/1 -> Ok!
List GetAllTalesByCategory(string categoryName) = api/tales/kids -> Ok!
Tale GetTalesByCategoryAndId(string categoryName, int id) = api/tales/kids/1 -> Ok!
İf u wonder my ApiController
[HttpGet]
public Tale GetAllTalesByCategoryAndId(string category, int id){}
[HttpGet]
public IEnumerable<Tale> GetAllTalesByCategory(string category){}
[HttpGet]
public IEnumerable<Tale> GetAllTales(){}
[HttpGet]
public Tale GetTale(int id){}
Thanks for all replies.
You need to switch the route order. Default route will handle the request when you don't specify an id (api/tales) so you need to place your custom route before that.
config.Routes.MapHttpRoute(
name: "ApiCategory",
routeTemplate: "api/tales/{category}/{id}",
defaults: new { controller = "Tales",
id = RouteParameter.Optional,
category = RouteParameter.Optional});
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = #"^[0-9]+$" }
);

Asp.net Web API : No action was found on the controller

I am using Visual Studi0 2010. In my WebApiConfig.cs file I have below config, I can map these two urls /Values and /Values/1. It is working fine.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id= RouteParameter.Optional }
);
Since I wanted to my custom URL like /Values/Machines and /Values/Machines/100, I changed above setting to below.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {action="get", id= RouteParameter.Optional }
);
It is now mapping below urls except this one, /Values/1. Please let me know what I am missing.
1./Values
2./Values/Machines
3./Values/Machines/100
Thanks.
It is expected behavior. According to your new Route the first parameter Values is expected to be a Controller name, and second parameter 1 is expected to be an Action, but you don't have an action with the name 1
In order to get to the Action you need the url with you new route should be like so: /Values/Index/1
You could do something like this:
routes.MapRoute(
name: "IndexWithParam",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { id = #"\d+" }
);
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In this case if the Url is like this: /Values/1, it will hit first route and redirect it to /Values/Index/1. Note that I've placed a constraint on this route that makes sure that second parameter is a number.

Resources