Clean URLs in Umbraco 13
# help-with-umbraco
t
Hi In previous versions of Umbraco I used to use the UrlRewriting file to get an URL which was built like /GetCustomer?id=2ยฎion=1 This was cleaned up to look like /GetCustomer/2/1/John What would be the correct way to do this in Umbraco 13? Thank you
a
If you want it to work that way I think you need to make a custom route for it
Something like
Copy code
csharp
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "getCustomer",
        pattern: "GetCustomer/{id}/{region}/{name}",
        defaults: new { controller = "YourControllerName", action = "GetCustomer" }
    );
   
});
Then you could make a controller taking in those params and return the correct view for it
You could also add an UrlRewriting middleware rule that rewrites URLs from /GetCustomer/2/1/John to /GetCustomer?id=2ยฎion=1&name=John.
I cant find a correct doc link for it, but something like this:
Copy code
csharp
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {   
        app.UseRewriter(new RewriteOptions()
            .AddRewrite(@"^GetCustomer/(\d+)/(\d+)/(\w+)$", "GetCustomer?id=$1ยฎion=$2&name=$3", skipRemainingRules: true)
            // Add more rewrite rules if needed
        );

        // Other middleware configurations
    }
t
It seems it's become a tad more complex. So middleware is the approach to take I assume. I'll take the last bit of the code you provided for demo purposes but if you do find the documentation link that would really help too ๐Ÿ‘
a
Well its basically default .net core functionality, not Umbraco specific, so: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-8.0
Guess that should help ? ๐Ÿ™‚
t
Thanks, I'll have a read into that ๐Ÿ‘
m
Although if you are running on iis, MS recommends using IIS urlrewrite ๐Ÿ˜‰
Also forgive me if I'm wrong... but could that be an api endpoint? which you could configure with attribute routing?
m
https://docs.umbraco.com/umbraco-cms/implementation/custom-routing#routingrequestnotification likely youi could do the url rewriting using this notification. Never tried it though
52 Views