Configuring Route to allow dot as extension?

不想你离开。 提交于 2019-12-11 07:28:55

问题


Imagine the following:

client app, which no one has control over, calls Web API methods but each of these methods ends in extensions such as :

/api/{controller}/{method}.ab

How is it possible to configure the ASP.NET web API, for a specific controller, to allow extensions for the method being called but also allow the existing default controllers to continue working?

For instance if we have a method call GetCustomer, the client app will call GetCustomer.ab but want that call to be routed to GetCustomer which is the method in the controller "Customer", so something like this:

http://fake.com/api/SmallService/GetCustomer.ab

SmallService is the specific controller.

There are other controllers but only want to apply this type of routing to a specific controller.

Enabling the RAMMFAR is not an option either (RunAllManagedModulesForRequests) due to performance and security reasons too.

This is for ASP.NET Web API 2 running on .NET 4.6


回答1:


First, configure web.config to allow the requests with specific file extension to pass on to the mvc framework.

The problem is that IIS will handle the .ab file as a static file and will by default not route the hypothetical .ab file through your MVC application. IIS handles the request and your MVC code never gets a chance to route to this file.

In summary, here's what the configuration looks like to make .ab files work:

<system.webServer>
  <handlers>
    <!-- other handlers -->

    <add name="ABHandler"
      path="*.ab"
      verb="GET" type="System.Web.Handlers.TransferRequestHandler"
      preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0"  />

    <!-- other handlers -->
  </handlers>
</system.webServer>

Do take note of the allowed verbs in the handler. update accordingly to suit the needs of the service.

Next, if requests like these are meant for only one controller then map the route to the intended controller.

if the actions all belonged to a specific controller then a convention-based approach could applied.

public static class WebApiConfig 
    public static void Register(HttpConfiguration config) {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.

        config.Routes.MapHttpRoute(
            name: "SmallServiceApi",
            routeTemplate: "api/SmallService/{action}.ab",
            defaults: new { controller = "SmallService" }
        );

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

there is also another way via attribute routing.

[RoutePrefix("api/SmallService"]
public class SmallServiceController : ApiController {
    //GET api/SmallService/GetCustomer.ab
    [HttpGet]
    [Route("GetCustomer.ab")]
    public IHttpActionResult GetCustomer() { ... }

}

Drawback to this approach, depending on your preference, it that you have to tag all the actions on their respective controllers and if there are many then this can become very repetitive.



来源:https://stackoverflow.com/questions/40561302/configuring-route-to-allow-dot-as-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!