All ASP.NET Web API controllers return 404

前端 未结 19 1641
臣服心动
臣服心动 2020-12-08 01:48

I\'m trying to get an API Controller to work inside an ASP.NET MVC 4 web app. However, every request results in a 404 and I\'m stumped. :/

I have th

相关标签:
19条回答
  • 2020-12-08 02:10

    I have been working on a problem similar to this and it took me ages to find the problem. It is not the solution for this particular post, but hopefully adding this will save someone some time trying to find the issue when they are searching for why they might be getting a 404 error for their controller.

    Basically, I had spelt "Controller" wrong at the end of my class name. Simple as that!

    0 讨论(0)
  • 2020-12-08 02:11

    If you manage the IIS and you are the one who have to create new site then check the "Application Pool" and be sure the CLR version must be selected. In my situation, it had been selected "No Managed Code". After changed to v4.0 it started to work.

    0 讨论(0)
  • 2020-12-08 02:12

    I had the same 404 issue and none of the up-voted solutions here worked. In my case I have a sub application with its own web.config and I had a clear tag inside the parent's httpModules web.config section. In IIS all of the parent's web.config settings applies to sub application.

    <system.web>    
      <httpModules>
        <clear/>
      </httpModules>
    </system.web>
    

    The solution is to remove the 'clear' tag and possibly add inheritInChildApplications="false" in the parent's web.config. The inheritInChildApplications is for IIS to not apply the config settings to the sub application.

    <location path="." inheritInChildApplications="false">
      <system.web>
      ....
      <system.web>
    </location>
    
    0 讨论(0)
  • 2020-12-08 02:13

    I'm going to add my solution here because I personally hate the ones which edit the web.config without explaining what is going on.

    For me it was how the default Handler Mappings are set in IIS. To check this...

    1. Open IIS Manager
    2. Click on the root node of your server (usually the name of the server)
    3. Open "Handler Mappings"
    4. Under Actions in the right pane, click "View ordered list"

    This is the order of handlers that process a request. If yours are like mine, the "ExtensionlessUrlHandler-*" handlers are all below the StaticFile handler. Well that isn't going to work because the StaticFile handler has a wildcard of * and will return a 404 before even getting to an extensionless controller.

    So rearranging this and moving the "ExtensionlessUrlHandler-*" above the wildcard handlers of TRACE, OPTIONS and StaticFile will then have the Extensionless handler activated first and should allow your controllers, in any website running in the system, to respond correctly.

    Note: This is basically what happens when you remove and add the modules in the web.config but a single place to solve it for everything. And it doesn't require extra code!

    0 讨论(0)
  • 2020-12-08 02:14

    I had the same problem, then I found out that I had duplicate api controller class names in other project and despite the fact that the "routePrefix" and namespace and project name were different but still they returned 404, I changed the class names and it worked.

    0 讨论(0)
  • 2020-12-08 02:15

    One thing I ran into was having my configurations registered in the wrong order in my GLobal.asax file for instance:

    Right Order:

    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    

    Wrong Order:

    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    

    Just saying, this was my problem and changing the order is obvious, but sometimes overlooked and can cause much frustration.

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