__Scenario__:
User likes to register a per-route message handler for the Delete Customer action in the following controller.
```
[RoutePrefix("api/Customers")]
public class CustomersController : ApiController
{
[HttpDelete("{id}")]
public string Delete(int id)
{
return "DeleteCustomer with id " + id;
}
}
```
__Issue__:
HttpDirectRoute doesn't have a constructor which accepts HttpMessageHandler. I need this as the Handler property on HttpRoute (which HttpDirectRoute derives from) has a 'private set'
For reference, before the recent addition of HttpDirectRoute, we could add the per-route message handler like below:
```
public class CustomHttpRouteBuilder : HttpRouteBuilder
{
public CustomHttpRouteBuilder(HttpConfiguration configuration)
{
this.Configuration = configuration;
}
public HttpConfiguration Configuration { get; private set; }
public override IHttpRoute BuildHttpRoute(HttpRouteValueDictionary defaults, HttpRouteValueDictionary constraints, string routeTemplate)
{
if (defaults["controller"].ToString().ToLowerInvariant() == "customers" &&
defaults["action"].ToString().ToLowerInvariant() == "delete")
{
return new HttpRoute(routeTemplate, defaults, constraints,
dataTokens: null,
handler: HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(Configuration),
new DelegatingHandler[] { new DeleteCustomerRouteDelegatingHandler() }));
}
return base.BuildHttpRoute(defaults, constraints, routeTemplate);
}
}
```
Register the custom route builder with configuration:
```
config.MapHttpAttributeRoutes(new CustomHttpRouteBuilder(config));
```
__Workaround__:
User has to create a custom route type deriving from HttpRoute, which accepts HttpMessageHandler and also mimic the functionality of HttpDirectRoute within it. User can then return the custom route type based instances when HttpRouteBuilder is building the routes.
Comments: We recommend using Web API action filters for these types of concerns. Action filters can be applied globally, per-controller, or per-action. That action filter would then have logic in it to decide whether it should apply to the current request (perhaps based on some data from the route, such as the existence of a parameter, or a route parameter having a particular value). Thanks, Eilon
User likes to register a per-route message handler for the Delete Customer action in the following controller.
```
[RoutePrefix("api/Customers")]
public class CustomersController : ApiController
{
[HttpDelete("{id}")]
public string Delete(int id)
{
return "DeleteCustomer with id " + id;
}
}
```
__Issue__:
HttpDirectRoute doesn't have a constructor which accepts HttpMessageHandler. I need this as the Handler property on HttpRoute (which HttpDirectRoute derives from) has a 'private set'
For reference, before the recent addition of HttpDirectRoute, we could add the per-route message handler like below:
```
public class CustomHttpRouteBuilder : HttpRouteBuilder
{
public CustomHttpRouteBuilder(HttpConfiguration configuration)
{
this.Configuration = configuration;
}
public HttpConfiguration Configuration { get; private set; }
public override IHttpRoute BuildHttpRoute(HttpRouteValueDictionary defaults, HttpRouteValueDictionary constraints, string routeTemplate)
{
if (defaults["controller"].ToString().ToLowerInvariant() == "customers" &&
defaults["action"].ToString().ToLowerInvariant() == "delete")
{
return new HttpRoute(routeTemplate, defaults, constraints,
dataTokens: null,
handler: HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(Configuration),
new DelegatingHandler[] { new DeleteCustomerRouteDelegatingHandler() }));
}
return base.BuildHttpRoute(defaults, constraints, routeTemplate);
}
}
```
Register the custom route builder with configuration:
```
config.MapHttpAttributeRoutes(new CustomHttpRouteBuilder(config));
```
__Workaround__:
User has to create a custom route type deriving from HttpRoute, which accepts HttpMessageHandler and also mimic the functionality of HttpDirectRoute within it. User can then return the custom route type based instances when HttpRouteBuilder is building the routes.
Comments: We recommend using Web API action filters for these types of concerns. Action filters can be applied globally, per-controller, or per-action. That action filter would then have logic in it to decide whether it should apply to the current request (perhaps based on some data from the route, such as the existence of a parameter, or a route parameter having a particular value). Thanks, Eilon