5

I am trying to add the Web API to an existing ASP.NET MVC project. This was originally a ASP.NET MVC 2 web site that 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

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

My Api controller is pretty straightforward. Actually, it is 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've added a class to register de 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 }
        );
    }
}

That 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 properly, the web api route is the first entry that 'Matches Current Request'.

6
  • Is your controller an inner (or nested) class? Web API controller selector does not check for inner classes. Mar 28, 2013 at 16:32
  • No, it's not an inner class.
    – igork
    Mar 28, 2013 at 17:45
  • 1
    In which namespace is your api controller class? Just wondering if it might be a conflict between an mvc controller and api controller like in this SO post: stackoverflow.com/questions/10677745/…
    – AardVark71
    Mar 29, 2013 at 13:30
  • 2
    Thanks @AardVark71! I've changed the namespace for the api controller and now it's working. Although I don't have another controller with the same name, so I'm not sure what was the conflict.
    – igork
    Apr 1, 2013 at 12:44
  • The namespace was the cause of the issue for me, however, it was the namespace of the inherited class and not the controller itself. Weird?
    – JDandChips
    Feb 9, 2014 at 12:16

4 Answers 4

2

Please see below steps - it may not be give you a right answer however it will give you a direction to debug problem. You can perform these actions one by one.

  1. Make sure that you have executed the following:

    Install-Package Microsoft.AspNet.WebApi
    

This is just to ensure that you have right DLLS.

  1. Another thing that you can try with is add a route on the top of a 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 am giving you below from my project as a reference.

 <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>
1
  • Thanks, I have updated the dlls and its working fine.
    – Sami
    Oct 16, 2015 at 18:36
2

The solution was to give the apicontroller class a different namespace, like AardVark71's suggestion in the comments.

The existing MVC had a namespace like WebApplication.Controllers, and the web api was using that same namespace. When I changed it, everything worked.

The complete 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

Make sure you're using the same version of

 System.Web.Http

I came across this error when I had a BaseApiController System.Web.Http v4.0 being inherited from an ApiController System.Web.Http v5.0

0
0

Same error gived me by asp.net web api but my solved that problem with different method and that method;

First Method

I changed controller name with AclController to AuthorizeController and solved my problem.

Second Method

Maybe this method solve your problem if you use method parameters type with string, int, bool etc. types then takes this error and don't use string, int, bool etc. types only use class or interface etc. types then maybe solved your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.