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

Edited 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.

Viewing all articles
Browse latest Browse all 7215

Trending Articles



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