question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Outbound route parameter transformer not working on ASP.NET Core MVC unless I use attribute routing

See original GitHub issue

Describe 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, Dec 12, 2018

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

0reactions
BrightSoulcommented, Dec 12, 2018

Ok, cool, I’m closing this issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found