Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Commented Issue: Provide HttpDirectRoute constructor which accepts HttpMessageHandler [1155]

$
0
0
__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: Here in my company we have a lot of Handlers that we share between several Apps. The goal is to compose your HTTP Pipeline based on what you need. Need to save metrics in our Log Server? Just use MetricsHandler. Need to load the current user context information into the Request? Just use the AccountLoadingHandler. There are two main reasons why the proposed workaround will not work for us: 1) We cannot ensure execution order with ActionFilter; 2) ActionFilter's methods are not async (and we do I/O inside most of our Handlers). Frankly, HttpMessageHandler is a great way of reusing logic, and I think support to such a powerful tool should not be discontinued.

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>