문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top