Outbound route parameter transformer not working on ASP.NET Core MVC unless I use attribute routing
See original GitHub issueDescribe the bug
An outbound route parameter transformer works only when I use attribute routing. Instead, I think it makes more sense if it worked even with the default route.
To Reproduce
Register an outbound route parameter transformer like this.
services.AddMvc(options =>
{
var routeParameterTransformer = new MyParameterTransformer();
var routeConvention = new RouteTokenTransformerConvention(routeParameterTransformer);
options.Conventions.Add(routeConvention);
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
Where MyParameterTransformer
is an implementation of IOutboundParameterTransformer
.
public class MyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value as string == "Home") {
return "Welcome";
}
return value?.ToString();
}
}
Now, when I visit the path /Welcome
I get a 404 when I should be able to see the homepage instead. It starts working only if I use [Route("[controller]")]
on the HomeController
, which is inconvenient for UI applications.
Expected behavior
I’d expect the route parameter transformer to work with any route defined in the Startup
class but it seems I must rely on attribute routing to make it work.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Outbound route parameter transformer not working on ASP ...
Outbound route parameter transformer not working on ASP.NET Core MVC unless I use attribute routing #4578. Closed. BrightSoul opened this issue ...
Read more >Asp.Net Core Routing using a parameter transformer and ...
I want to customize routes depending on the culture of web application to have a 'friendly' url (to optimize SEO).
Read more >Routing to controller actions in ASP.NET Core
NET Core MVC uses Routing Middleware to match URLs of incoming ... If the default order used for URL generation isn't working, using...
Read more >Using Parameter Transformers in Razor Pages Routing
Available from ASP.NET Core 2.2, parameter transformers enable the centralisation of logic for modifying the value of routes and route ...
Read more >ASP.NET Core Routing Basics
Routes are patterns which can be matched to given endpoints to redirect requests. Said patterns are used to parse any given URL to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The purpose of a transformer is to apply a consistent pattern to a parameter. For example, making the controller and action parameters use camel case.
Thanks for the feedback on the documentation. I’ve made changes here - https://github.com/aspnet/Docs/pull/9987
Ok, cool, I’m closing this issue.