ASP.NET MVC WebAPI 404 Error

I have an asp.net web form application running in integrated mode v4.0.

I tried adding apicontroller to the App_Code folder.

In Global.asax, I added the following code

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

When I tried to go to the controller in http://localhost/api/Value , I get a 404 error.

A URL without an extension is configured in the handler section. I have forms and anonymous authentication for a website.

ExtensionLess extension URL configured for '*.'

When I click the URL for the controller, the request is processed by StaticHandler instead of ExtensionlessUrlHandler-Integrated-4.0.

Now I have no idea why the system will throw an error, as shown in the figure below. Error

+61
c # asp.net-mvc asp.net-web-api
Dec 16 '13 at 22:01
source share
19 answers

I ran into this problem.

I tried editing my WebApiConfig.cs to follow a series of recommendations here and code examples elsewhere. Some worked, but did not explain why the route did not work when WebApiConfig.cs was encoded exactly according to the design of the MS Web template.

My actual problem was that when manually adding WebApi to my project, I did not follow the order order for the configuration from Global.asax

  protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // This is where it "should" be GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); // The WebApi routes cannot be initialized here. BundleConfig.RegisterBundles(BundleTable.Bundles); } 

I could guess why this is so, but I did not investigate further. At least it was not interesting.

+111
Oct 03 '14 at 1:19
source share

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

Add a link 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 } ); 
+32
Dec 30 '14 at
source share

Provide the following:

1.) Make sure your IIS is configured with .NET 4.5 or 4.0 if your web api is 4.5. 4.5 in IIS

run this command on the command line with administrator privileges

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

2.) Change the routing to

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

and make a request using Demo / Get (where demo is the name of your controller)

if 1.2 don't work try 3

3.) Add the following configuration to the web.config file

 <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer> 
+17
Jan 01 '14 at 18:10
source share

Also, make sure your controller ends with the name “Controller”, as in “PizzaPieController”.

+10
Jun 25 '14 at 16:00
source share

I tried all of the above and had the same problem. It turned out that the application pool created in IIS is -.net 2.0 by default. When I changed it to 4.0, it worked again

+7
Oct 26 '14 at 15:44
source share

Thanks Shannon, works great =>

My order in my Global.asax was:

 GlobalConfiguration.Configure(WebApiConfig.Register); RouteConfig.RegisterRoutes(RouteTable.Routes); 

instead of good:

 RouteConfig.RegisterRoutes(RouteTable.Routes); GlobalConfiguration.Configure(WebApiConfig.Register); 
+4
Mar 17 '16 at 12:49
source share

Also try deleting all the contents of the api bin folder. Mine contained the old dlls (due to the large renaming of the namespace), exposing conflicting controllers. These DLLs have not been removed using the Visual Studio Clean features.

(However, I find that asp.net web api seriously does not have routing and debugging information at the debug level).

+3
May 25 '15 at 21:53
source share

If you create a controller in App_Code, how does the routing table know where it is? You specified the route as "api / {controller / ...", but not where the controller is located. Try moving it to the desired folder.

+2
Jan 02
source share

After hours of wasting time on this, I found a solution to this in my case.

This was the procedure for registering routes in RouteConfig .

We must register HttpRoute in the Route table before the default controller route. This should be next. Route Config Table Configuration

  public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 
+2
Aug 31 '17 at 4:15
source share

For the URL you are trying ( http://localhost/api/Value ), make sure that there is a public type called ValueController that comes from ApiController and has a public method with some of these characteristics:

  • The method name begins with Get (for example, GetValues or just Get ).
  • This method uses the HttpGet attribute.

If you are trying to use the code from the default web API project template, the name of the controller is ValuesController , not ValueController , so the URL will be http://localhost/api/values .

If this does not help, you can enable tracing , which can give you a useful idea of ​​where the error occurs in the pipeline (and also why).

Hope this helps.

+1
Dec 27 '13 at 16:32
source share

I copied the dll of the controller based on RouteAttribute to the bin folder, but it was not recognized as a valid controller, and I was getting a 404 error on the client.

After a lot of debugging, I found my problem. This is because the version of System.Web.Http.dll referenced by the controller was different from the version of System.Web.Http.dll , referring to the main project (the one that contains global.asax.cs).

Asp.Net finds the controller by reflection using code like this

 internal static bool IsControllerType(Type t) { return t != null && t.IsClass && t.IsVisible && !t.IsAbstract && typeof(IHttpController).IsAssignableFrom(t) && HasValidControllerName(t); } 

Since IHttpController is different for each version of System.Web.Http.dll , the controller and the main project must have the same link.

+1
Oct 23 '15 at 18:09
source share

We also had this; changing the .NET version from 4.5 to 4.5.1 or a newer problem was resolved.

+1
Dec 02 '15 at 7:16
source share

Try using the Value part of the controller name, for example:

 http://localhost/api/Value 



Note. By convention, the routing mechanism takes on the value passed as the name of the controller and adds the word Controller to it. By placing the ValueController in the URI, you have a routing mechanism for the class named ValueControllerController , which it did not find.

0
Dec 16 '13 at 22:06
source share

Your route configuration looks good. Double check the handlers section in web.config, for integrated mode this is the correct way to use ExtensionLessUrlHandler:

 <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 

More on this topic: http://blogs.msdn.com/b/tmarq/archive/2010/05/26/how-extensionless-urls-are-handled-by-asp-net-v4.aspx

0
Jan 02
source share

None of the solutions above solved my problem ... My mistake was that I copied the bin files directly to the production server, and then, I do not work. 404 disappeared when I published the project to disk and copied the “published” folder to the server. This is a little obvious, but may help someone.

0
Jun 09 '15 at 11:13
source share

Time for me to add my stupid supervision to the list here: I made a mistake along the default route path for webapi.

Original:

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

Fixed: (watch curly braces around "id")

 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional} ); 
0
Sep 25 '15 at 2:13
source share

I understand that this is a very old question, but I thought I would add another answer for future users.

I found that this only happened now in the project I was working on, only after it was deployed to CI / Staging. The solution was to toggle the compile debug value = true back and forth when deploying each version to each environment once, and it will be fixed for me.

0
Jul 18 '16 at 8:57
source share

In my case, I forgot to do this from ApiController.

So it will look like

 public class ValuesController : ApiController 
0
Jan 04 '17 at 18:30
source share

The route registration sequence was a problem in my Application_Start (). the sequence that worked for me was

 AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); RouteConfig.RegisterRoutes(RouteTable.Routes); 

it used to be

 AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); GlobalConfiguration.Configure(WebApiConfig.Register); 
0
Sep 08 '19 at 16:24
source share



All Articles