asp.net webapi 2 attribute routing not working

2019-01-31 11:59发布

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


    }
}

8条回答
Deceive 欺骗
2楼-- · 2019-01-31 12:38

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-31 12:39

This question already has a selected answer. But I had a different solution for myself and think it would be helpful to reply if the selected answer doesn't help.

For me it was a silly mistake. I had two controllers but only one was working. The solutions was that my controller class was named improperly!

My working controller-

public class FooController : ApiController { }

My non-working controller-

public class BarControllers : ApiController { }

Be sure your controller class ends in Controller. The trailing s got me!

查看更多
别忘想泡老子
4楼-- · 2019-01-31 12:40

In my case following line was creating problem, just commented it and everything start working

config.MapHttpAttributeRoutes();

Comment it in WebApiConfig.cs file

查看更多
疯言疯语
5楼-- · 2019-01-31 12:41

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);
}
查看更多
Deceive 欺骗
6楼-- · 2019-01-31 12:42

This was not your case (as is apparent from your sample code), but please do remember to end the Controller class name with Controller.

Else it won't be picked up by config.MapHttpAttributeRoutes();.

查看更多
家丑人穷心不美
7楼-- · 2019-01-31 12:45

I had this problem too and after a long search I realized that I was using System.Web.Mvc.RouteAttribute instead of System.Web.Http.RouteAttribute After correcting this and using config.MapHttpAttributeRoutes() everything worked fine.

查看更多
登录 后发表回答