How do I get ASP.NET Web API working with versioning and the Help Page extension?

前端 未结 3 1204
北恋
北恋 2021-01-31 10:28

I\'ve implemented a versioning framework into my WebAPI application, and would very much like to get it working with the new Help Page extension from Microsoft.

Microsof

3条回答
  •  灰色年华
    2021-01-31 10:47

    I agree with @mortware, the default routing for web api would mean your url should look something like "site/api/controllerName/" if you're using default Get()/Post() methods. If you're using specificly named methods then the route looks something like "site/api/controllerName/methodName".

    I've also run into difficulty with the parameter names. Eg, if in your route specified in /App_Start/WebApiConfig.cs you have;

    // Controller with ID
    // To handle routes like `/api/VTRouting/1`
    config.Routes.MapHttpRoute(
        name: "ControllerAndId",
        routeTemplate: "api/{controller}/{id}",
        defaults: null,
        constraints: new { id = @"^\d+$" } // Only integers 
    );
    
    // Controllers with Actions
    // To handle routes like `/api/VTRouting/route`
    config.Routes.MapHttpRoute(
        name: "ControllerAndAction",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: null,
        constraints: new { id = @"^\d+$" } // Only integers 
    );
    

    then the method parameter for your http verb must have a parameter called "id", eg;

    // url: site/api/controller/
    public HttpResponseMessage Get(int id) { return null; /*dummy*/ }
    
    // url: site/api/controller/
    public HttpResponseMessage Post(int id) { return null; /*dummy*/ }
    
    // url: site/api/controller/SomeAction/
    public HttpResponseMessage SomeAction(int id) { return null; /*dummy*/ }
    

    If you have something like;

    public HttpResponseMessage Get(int myID) { return null; /*dummy*/ }
    

    It will not work as the "myID" parameter doesn't match that {id} specified in the route. As @OakNinja pointed out, we'll need your routing in the WebApiConfig.cs to help you pinpoint the exact cause

提交回复
热议问题