Following is an example of how one can achieve per-route message handlers. We should see if we can improve the experience of this.
Scenario:
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;
}
}
```
Create a custom http route builder.
```
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));
```
Also, if a user has defined his own custom attribute (probably which implements a custom IHttpRouteInfoProvider interface which could take in array of types of delegating handlers that the users want to chain), the default route builder should pass in this IHttpRouteInfoProvider reference to the BuildHttpRoute methods so that the end user can get these types and instantiate the handlers, which is better than the conditional way of checking above.
Comments: Let's wait until we have a concrete customer scenario for this. At the moment this is a more advanced scenario and people who want to implement this can just use the code in this bug :)
Scenario:
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;
}
}
```
Create a custom http route builder.
```
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));
```
Also, if a user has defined his own custom attribute (probably which implements a custom IHttpRouteInfoProvider interface which could take in array of types of delegating handlers that the users want to chain), the default route builder should pass in this IHttpRouteInfoProvider reference to the BuildHttpRoute methods so that the end user can get these types and instantiate the handlers, which is better than the conditional way of checking above.
Comments: Let's wait until we have a concrete customer scenario for this. At the moment this is a more advanced scenario and people who want to implement this can just use the code in this bug :)