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?

6
  • Do you see any other message in the body of the 404 response? Commented Sep 23, 2013 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. Commented Sep 25, 2013 at 16:52
  • 1
    yeah..right...the routes look correct and this should have worked... Commented Sep 25, 2013 at 16:57
  • BaseApiController is derived from ApiController (and not from Controller), I take it? Just making sure. Commented Sep 27, 2013 at 7: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. Commented Sep 30, 2013 at 15:49

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
And this answer is important too, make sure the webapi routes are first or it wont work! stackoverflow.com/questions/22401403/…
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.