Search code examples
c#asp.net-mvc-routingasp.net-core-2.1

ASP.NET Core MVC route to url with parameters


I have created a web application using ASP.NET Core 2.1 MVC. And currently i am having an issue with routing when the application is published. The url format where the application will be published is this : https://hostername.com/{some_parameter}

All the generated url-s from my application should be "attached" to the above mentioned url.

So i need to have a routing like this :

https://hostername.com/{some_parameter}/{controller}/{action}/{id}

Some examples :

 - https://hostername.com/ApplicationName/Home/Profile
 - https://hostername.com/ApplicationName/Home/Settings
 - https://hostername.com/ApplicationName/FAQ etc... 

My solution for this after reading couple of questions/solutions on stackoverflow : Changed the default route to

routes.MapRoute(
                name: "default",
                template: $"{{parameter={settings.PrefixURL}}}/{{controller=Home}}/{{action=Index}}/{{id?}}");

where settings.PrefixURL => it's the some_parameter and it's value it's dynamic.

Problem i am facing => Doubled parameters in url, for example :

- https://hostername.com/ApplicationName/Home/Home/Profile
- https://hostername.com/ApplicationName/ApplicationName/FAQ

When tested locally that configuration of default routing worked perfectly, but after publishing the routing still works but the url is wrong. What could be causing the issue? Would creating Areas solve the routing to that kind of url?

Thank you.


Solution

  • What did the work for me was editing the path on web.config file and leaving the rest of soultion untouched. From path="*" to path="/ApplicationName" where ApplicationName is the desired routing parameter.