Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net webapi 2 attribute routing not working

I have visual studio 2012 installed with mvc4 using .net framework 4.5. Now I want to use webapi2 with attribute writing and i want my hlep page show all the endpoints properly.

In my solution i added a new mvc4 base emtpy project and using nuget i upgraded to mvc5 and then i have installed webapi2 packages. lastly i have installed help package for webapi2.

now when i use routeprefix I cant see any content on help page and when i try to access my webapi endpoint in browsers it throws following error.

http://expressiis.com/api/v1/

   <Error>     <Message>     No HTTP resource was found that matches the request URI 'http://expressiis.com/api/v1/'.     </Message>     <MessageDetail>     No type was found that matches the controller named 'v1'.     </MessageDetail>     </Error>  namespace WebApi.Controllers {     [RoutePrefix("api/v1")]     public class SubscribersController : ApiController     {         // GET api/<controller>            [Route("")]         [HttpGet]         public IQueryable<string> Get()         {             return new string[] { "value1", "value2" }.AsQueryable();         }       } } 
like image 695
najam Avatar asked Nov 11 '13 13:11

najam


People also ask

What types of routing does Web API 2 support?

Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

How do I add routing to Web API?

The default route template for Web API is "api/{controller}/{id}". In this template, "api" is a literal path segment, and {controller} and {id} are placeholder variables. When the Web API framework receives an HTTP request, it tries to match the URI against one of the route templates in the routing table.


2 Answers

Based on your information, it looks like you are not calling the httpConfig.MapHttpAttributeRoutes() (Make sure to call this before any traditional routing registrations)

Since you haven't called MapHttpAttributeRoutes, your request seems to be matching a traditional route, for example, like api/{controller}. This will not work because routes matching traditional routes will never see controllers/actions decorated with attribute routes.

like image 74
Kiran Avatar answered Sep 18 '22 14:09

Kiran


A problem I ran into was related to the ordering in Application_Start(). Note the order of Web API configuraton below:

This does NOT work

protected void Application_Start() {     AreaRegistration.RegisterAllAreas();     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);     RouteConfig.RegisterRoutes(RouteTable.Routes);     BundleConfig.RegisterBundles(BundleTable.Bundles);     GlobalConfiguration.Configure(WebApiConfig.Register); } 

This does work

protected void Application_Start() {     AreaRegistration.RegisterAllAreas();     GlobalConfiguration.Configure(WebApiConfig.Register);     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);     RouteConfig.RegisterRoutes(RouteTable.Routes);     BundleConfig.RegisterBundles(BundleTable.Bundles); } 
like image 43
Josh C Avatar answered Sep 17 '22 14:09

Josh C