MVC Routes are not filesystem-based like old-style ASP.NET sites. All the controllers are registered at compile and the route just picks the one with the matching name. Where the controller lives on your filesystem is irrelevant.
How can I get Advanced Url Routing parametres?
-
18-03-2022 - |
Question
It works now like localhost/Controller/Action but I want it like localhost/MainFolder/SubFolder/Controller/Action
because I need to get my MainFolder and SubFolder name; like
RouteData routeData = htmlHelper.ViewContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentMainFolder = routeData.GetRequiredString("mainFolder");
string currentSubFolder = routeData.GetRequiredString("subFolder");
MyViewEngine Codes;
public class MyViewEngine: RazorViewEngine
{
private static string[] NewViewFormats = new[] { "~/Views/MainFolder/SubFolder/{1}/{0}.cshtml" };
public MyViewEngine()
{
base.ViewLocationFormats = base.ViewLocationFormats.Union(NewViewFormats).ToArray();
}
}
My RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Panel", action = "Index", id = UrlParameter.Optional }
);
ViewEngines.Engines.Add(new MyViewEngine());
}
My Views Folder;
My Controllers Folder;
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow