Asp.net MVC route config

First, I'm not a developer (ui designer), but I'm having to work with some asp.net, well, because life...so I know little to nothing.

I have a structure like this:

Views > Partners >

and inside the "Partners", I have over 40 files named in CamelCase, like FileName.cshtml etc...

I need to re-route the url so instead of "website/Partners/PartnerName", it would be like this "website/partners/partner-name"

I tried tons of tutorials, but I just cannot understand what needs to be done to achieve this. The closest I got to a solution, was using the "[Route("partners/partner-name")]" inside the Partners controller. But for this solution, I have to add that line of code to all of the 40 files (gonna have more than a hundred in the future). I believe there must be some way to automatically do it for all the pages I have, instead of manually editing each one of them.

Again, I'm not a developer, I'm just having to deal with this right now, and I know almost nothing about .net. Thanks.

1 answer

  • answered 2021-01-20 10:57 silverfighter

    I guess 4.6.1 is the .Net-Framework. So I just shoot an answer which applies to MVC 5 and we see how good it fits for you. (Because Versioning is tricky - confusing lately in the dotnet space).

    You can override routing in a file called RouteConfig.cs

    routes.MapRoute(
                name: "OverrideRoute",
                url: "websites/partners/{partner-name}",
                defaults: new { controller = "AnController", action ="AnAction" }
        );
    

    Beware the order matters first-match wins. Also this is set up do act like /websites/partners/contosocompany if you want especially websites/partners/partner-name/contosocompany (<- less nice and readable) you would need to adjust the url property in MapRoute

    It should also be possible to group routes see this answer here: Asp.net MVC 5 MapRoute for multiple routes

    And please talk to your developers if you have certain sections which are almost like individual mvc pages you could also take advantage of a concept called areas. For example your partners section could be an area. https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-5.0

    ... and keep in mind if you are a designer who can look into code, read and change it - you got already a business advantage in our industry.