asp.net webapi 2 attribute routing not working

后端 未结 9 1930
再見小時候
再見小時候 2020-12-23 17:02

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 prope

相关标签:
9条回答
  • 2020-12-23 17:25

    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);
    }
    
    0 讨论(0)
  • 2020-12-23 17:28

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

    0 讨论(0)
  • 2020-12-23 17:29

    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.

    0 讨论(0)
  • 2020-12-23 17:37

    In my case, VS create my controller with the name

    TestController1
    

    I dont know why he put this number "one" in the end of name, but remove and will work.

    0 讨论(0)
  • 2020-12-23 17:37

    In my case, I was missing full custom path in attributes. I was writing only custom action name without 'api/'. So that was my mistake. My scenario was, WebApiConfig template code:

            // Web API routes
            config.MapHttpAttributeRoutes();
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    

    my incorrect way of route

    [RoutePrefix("myapps")]  // wrong code
    public class AppsController : BaseRestAPIController
    {
        [HttpPost]
        [Route("getapps")]
        public ResponseData GetAppList()
        {
    

    Correct way

    [RoutePrefix("api/myapps")]  // correct way. full path start from 'api/'
    public class AppsController : BaseRestAPIController
    {
        [HttpPost]
        [Route("getapps")]
        [Route("api/myapps/getapps")] // you can use full path here, if you dont want controller level route
        public ResponseData GetAppList()
        {
    
    0 讨论(0)
  • 2020-12-23 17:37

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

    config.MapHttpAttributeRoutes();
    

    Comment it in WebApiConfig.cs file

    0 讨论(0)
提交回复
热议问题