ASP.NET Web API Error: HTTP Resource Matching Request URI Not Found

I am trying to add a web API to an existing ASP.NET MVC project. This was initially the ASP.NET MVC 2 website, which was later upgraded to MVC 3, and then again to MVC 4, using the following procedures:

Upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3

ASP.NET MVC 3 Project Update for ASP.NET MVC 4

My Api controller is pretty simple. This is actually the default template:

public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string Get(int id) { return "value"; } } 

I added a class for registering API routes.

 public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } 

This is called from Application_Start

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); RouteConfig.RegisterRoutes(RouteTable.Routes); } 

However, when I try to access "localhost: 50315 / api / values", I get the following error:

 <Error> <Message> No HTTP resource was found that matches the request URI 'http://localhost:50315/api/values'. </Message> <MessageDetail> No type was found that matches the controller named 'values'. </MessageDetail> </Error> 

According to the routing debugger, the route seems to be working correctly, the api web route is the first entry that โ€œMatches the current requestโ€.

+4
source share
4 answers

The solution was to give the apicontroller class a different namespace, such as the AardVark71 clause in the comments.

The existing MVC had a namespace, such as WebApplication.Controllers, and the web api used the same namespace. When I replaced him, everything worked.

The full Web Api controller looks something like this:

 namespace WebApplication.Api.Controllers { public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string Get(int id) { return "value"; } } } 
+1
source

See the steps below - this may not give you the correct answer, but it will give you direction to debug the problem. You can perform these actions one by one.

  • Make sure you do the following:

     Install-Package Microsoft.AspNet.WebApi 

This is just to make sure you have DLLS rights.

  1. Another thing you can try is to add a route at the top of get.

     class ValuesController : ApiController { [System.Web.Http.Route("api/values")] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } public string Get(int id) { return "value"; } } 
  2. Disable MvC route for debugging. i.e.

     protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); } 
  3. Enable GET Verb.

I give you below my project as a link.

  <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Headers" value="*" /> <add name="Access-Control-Allow-Methods" value="*" /> </customHeaders> </httpProtocol> <validation validateIntegratedModeConfiguration="false" /> <handlers> <remove name="WebDAV" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> <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" /> </handlers> 
+2
source

Make sure you are using the same version.

  System.Web.Http 

I ran into this error when I had BaseApiController System.Web.Http v4.0 inherited from ApiController System.Web.Http v5.0

+1
source

The same error gave me the asp.net web api, but I solved this problem with another method and this method;

First method

I changed the name of the controller using AclController to AuthorizeController and solved my problem.

Second method

Perhaps this method solves your problem if you use the method parameter type with types string, int, bool, etc., then takes this error and does not use the types string, int, bool, etc., only use the class or interface etc. can solve your problem.

0
source

All Articles