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;

enter image description here

My Controllers Folder;

enter image description here

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top