-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Outbound route parameter transformer should support dependency injection #4579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for contacting us, @BrightSoul. |
Hi @BrightSoul The To achieve what you want would require giving the type and the service provider to the constructor, and then resolving the type in the constructor. Doing that would beg the question, what is being saved over just resolving the service yourself and then calling the current constructor? I don't think this is something that we would add. However, the class isn't sealed. You could inherit from it, and add a constructor that does what I described above. |
Hey @JamesNK, thanks for your reply.
Yeah, I thought it would be more complex at first and perhaps my expectations for an "outbound parameter transformer" were wrong. However, as I've seen, an outbound route transformer is just for simple transformations.
Nothing, of course. As it is now, I could just inject its dependencies myself when I construct it. |
BTW, in classic ASP.NET I've been handling URL localization by hacking a route constraint to rewrite values in the RouteValuesDictionary, which is absolutely barbaric. I wonder if ASP.NET Core has a better solution to this. A custom middleware would solve the url resolution part but how about url generation? |
Unfortunately you can't do that with You can access the scoped HttpContext in a parameter transformer using IHttpContextAccessor when you want to transform non-controller/action/area/page routes values: public class SampleOutboundRouteTransformer : IOutboundParameterTransformer
{
private readonly IHttpContextAccessor _httpContextAccessor;
public SampleOutboundRouteTransformer(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string TransformOutbound(object value)
{
return value?.ToString();
}
} app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}",
defaults: null,
constraints: new { id = new SampleOutboundRouteTransformer(routes.ServiceProvider.GetRequiredService<IHttpContextAccessor>()) });
}); This would allow id value to be transformed per request using values from HttpContext. |
Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments. |
Is your feature request related to a problem? Please describe.
When I register an outbound route parameter transformer, I should be able to register it as a type and not an implementation. This way, I could leverage ASP.NET Core dependency injection. Infact, I might need to read configuration or the database to decide how to do transformations.
Describe the solution you'd like
I register the route parameter transformer like this.
Instead, I'd like to register it like this, by just providing its type.
This way, ASP.NET Core would inject any dependencies when constructing the transformer.
Describe alternatives you've considered
I don't want to use the ServiceProvider directly.
The text was updated successfully, but these errors were encountered: