Why doesn't this Api web route go up?

so in my webapi configuration 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 a 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 network advise all the time. What am I missing?

+4
source share
3 answers

Finally, I realized what was going on.

The one who originally connected WebApi was not actually called WebApiConfig.Register(GlobalConfiguration.Configuration); , therefore, of course, not one of the configurations I added affected Web Api. By adding this to the fixed issue.

+1
source

I tried these routes in a small api test web project and everything works fine.

But here is my advice. Try to share your solution in two different projects. The 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

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

Register these sites correctly on your web server of your choice, and you're done. This approach should work. Moreover, using this method, you avoid the situation when several controllers can be accessed by two URLs: api/someController and cp/someController

+1
source

Most likely, you do not have the proper httpHandlers configuration for your application. Try adding 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> 

Pay attention to the verb attribute, it must contain the PUT verb.

0
source

Source: https://habr.com/ru/post/1502919/


All Articles