Asp.net Web API : No action was found on the controller - asp.net-web-api

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.

Related

I want to set MVC route

I want to set routing, when the user will write a URL like www.abc.com/param1, it's auto-redirect or call to
www.abc.com/itemview/index/param1
www.abc.com/itemview/index/ is a fixed part.
I tried with the below route but it was not worked?
routes.MapRoute(name: "Default",
url: "ItemsViewNew/Index/{id}",
defaults: new { controller = "ItemsViewNew", action = "Index", id = UrlParameter.Optional }
);

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.

WEB API routing actions multiple

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}"
);

how to route this in mvc web api?

I have this requirement to map the url
api/v1/Contact/12/tags
in the above url the Contact is the controller, and 12 is the contactId and tags is the action is want to be called.
The method is like this
[HttpGet]
public List<TagModel> Tags([FromUri]int contactId)
I have did like this
config.Routes.MapHttpRoute(
name: "RouteForHandlingTags",
routeTemplate: "api/v1/{controller}/{id}/{action}",
defaults: new { },
constraints: new { id = #"\d+" }
);
I am getting the error "No HTTP resource was found that matches the request URI http://localhost:4837/api/v1/contact/12/tags"
I have tried
[HttpGet]
public List<TagModel> Tags(int contactId)
and
config.Routes.MapHttpRoute(
name: "RouteForHandlingTags",
routeTemplate: "api/v1/{controller}/{id}/{action}"
);
But all are giving the same error. But when I do a call like this
api/v1/Contact/12/tags?contactid=12
everything works fine. But this is not what I want. The contactId is specified after contact. Can anyone please tell me how to route this?
The parameter names in your action and route must match.
You can either change your action to:
[HttpGet]
public List<TagModel> Tags(int id)
Or change your route to:
config.Routes.MapHttpRoute(
name: "RouteForHandlingTags",
routeTemplate: "api/v1/{controller}/{contactId}/{action}",
defaults: new { },
constraints: new { id = #"\d+" }
);
Note: you do not need [FromUri] with a primitive type like an int.

Resources