4

so in my WebApi config I added a new route

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

and I have the controller

public class SwitchUserController : BaseApiController
{
    public HttpResponseMessage Put(int id) {
        return Request.CreateResponse(HttpStatusCode.OK);
    }   
}

and yet in chrome:

Request URL:http://localhost:1352/cp/SwitchUser/123
Request Method:PUT
Status Code:404 Not Found

I use default web api routing all the time. What am I missing?

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • Do you see any other message in the body of the 404 response? – Kiran Sep 23 '13 at 22:38
  • @KiranChalla Standard asp.net mvc 404 response. Are you saying that this is all configured correctly and should work? I haven't yet produced a reduced test case so I suppose it's possible that there is something else interfering with the routes - though I was hoping I just configured things wrong. – George Mauer Sep 25 '13 at 16:52
  • 1
    yeah..right...the routes look correct and this should have worked... – Kiran Sep 25 '13 at 16:57
  • `BaseApiController` is derived from `ApiController` (and not from `Controller`), I take it? Just making sure. – Marius Schulz Sep 27 '13 at 07:26
  • I remember getting an error like this once, where I only returned a status code. If you are not going to return anything else then you really should be returning a 204(No Content) response. – Excommunicated Sep 30 '13 at 15:49
  • @KiranChalla I just figured out what was going on. Whoever originally hooked up WebApi never actually called `WebApiConfig.Register(GlobalConfiguration.Configuration);` so of course.... – George Mauer Oct 01 '13 at 20:55

3 Answers3

1

I tried those routes in a small test web api project, and everything works just fine.

But here is my advice. Try to split your solution in two different projects. First config.file will have

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

Second one

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

Register these sites properly in your web server of choise, and you are done. This approach should work. Moreover, using this technique, you are avoiding the situation when multiple controllers can be accessed by two urls : api/someController and cp/someController

Max Tkachenko
  • 504
  • 1
  • 6
  • 17
1

I finally figured out what was going on.

Whoever originally hooked up WebApi never actually called WebApiConfig.Register(GlobalConfiguration.Configuration); so of course none of the configurations I added were affecting Web Api. Adding that in fixed the problem.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • Good answer!!! This happens when you add a web api controller to the mvc project. it creates the WebApiConfig file, but doestn' register it in the global.asax. – Byron Whitlock Apr 20 '15 at 20:15
  • 2
    And this answer is important too, make sure the webapi routes are first or it wont work! http://stackoverflow.com/questions/22401403/add-web-api-to-an-existing-mvc-web-application – Byron Whitlock Apr 20 '15 at 20:24
0

It's most likely that you do not have properly configured httpHandlers for your application. Try to add this to your web.config:

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />

  <handlers>
    <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,PUT,DELETE" 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,PUT,DELETE" 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,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>

Note the verb attribute, it should contain PUT verb.

Alexander Simonov
  • 1,564
  • 1
  • 9
  • 15