ASP.NET MVC WebAPI 404 error

前端 未结 8 1444
一生所求
一生所求 2020-12-13 23:31

I have an asp.net web forms application running under v4.0 integrated mode.

I tried to add an apicontroller in the App_Code folder.

In the Global.asax, I ad

相关标签:
8条回答
  • 2020-12-14 00:17

    The problem is in your routing configuration. Mvc routing is different from WebApi routing.

    Add reference to System.Web.Http.dll, System.Web.Http.Webhost.dll and System.Net.Http.dll and then configure your API routing as follows:

       GlobalConfiguration.Configuration.Routes.MapHttpRoute(
         name: "DefaultApi",
         routeTemplate: "api/{controller}/{id}",
         defaults: new { id = System.Web.Http.RouteParameter.Optional }
       );
    
    0 讨论(0)
  • 2020-12-14 00:18

    I tried all of the above and had the same problem. It turned out that the App pool created in IIS defaulted to .net 2.0. When I changed it to 4.0 then it worked again

    0 讨论(0)
  • 2020-12-14 00:25

    If you create the controller in App_Code how does the routing table know where it is? You have specified the route as "api/{controller/..." but that's not where the controller is located. Try moving it into the correct folder.

    | improve this answer | |
    0 讨论(0)
  • 2020-12-14 00:30

    Ensure the following things

    1.) Ensure that your IIS is configured with .NET 4.5 or 4.0 if your web api is 4.5 install 4.5 in IIS

    run this command in command prompt with administrator privilege

    C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -i
    

    2.) Change your routing to

    RouteTable.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "{controller}/{id}",
          defaults: new { id = System.Web.Http.RouteParameter.Optional }
     );
    

    and make request with Demo/Get (where demo is your controller name)

    if the 1,2 are not working try 3

    3.) Add following configuration in web.config file

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
    
    </system.webServer>
    
    0 讨论(0)
  • 2020-12-14 00:31

    Also try to delete your entire api bin folder's content. Mine was containing old dlls (due to a big namespace renaming) exposing conflicting controllers. Those dll weren't deleted by Visual Studio's Clean functionality.

    (However, I find asp.net web api seriously lacks routing and debugging information at the debugging level).

    0 讨论(0)
  • 2020-12-14 00:32

    Also, make sure your controller ends in the name "Controller" as in "PizzaPieController".

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