I have my personal blog site which was done using Asp.Net MVC and C# language.
As I have configured RouteConfig with "controller/action/{*Url}" for blog URL where {*Url} could be any text.
Now I want to change the Url pattern from "controller/action/{*Url}" to {*Url}.
Example:
From below Url
https://abcd.co.in/home/newsdetail/abcd-xyz-opqr-stu
To
https://abcd.co.in/abcd-xyz-opqr-stu
I have done the following changes
routes.MapRoute("", "mycontroller/myactionmethod/{*Url}", new { controller = "Home", action = "NewsDetail", Url = "" });
But it is calling every time URL hits my domain.
Please suggest what are the possible solutions that will not affect other functionality or URLs and it should fulfill my requirements.
Thanks in advance.
After a few google search and R&D on Attribute-based routing I have come up with one solution:
Steps:
Need to define that I am using Attribute routing
routes.MapMvcAttributeRoutes();
Remove existing route entry for the Action method.
Set Attribute route entry path above the Action method.
[Route("{Url}")]
Just update the URL in href from previous to just URL.
From
<a href="/home/newsdetail/@item.Url">
To
<a href="/@item.Url">