1

它现在像localhost/Controller/Action一样工作 ,但我希望它像localhost/MainFolder/SubFolder/Controller/Action

因为我需要获取我的 MainFolder 和 SubFolder 名称;喜欢

RouteData routeData = htmlHelper.ViewContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentMainFolder = routeData.GetRequiredString("mainFolder");
string currentSubFolder = routeData.GetRequiredString("subFolder");

MyViewEngine 代码;

    public class MyViewEngine: RazorViewEngine
{
    private static string[] NewViewFormats = new[] { "~/Views/MainFolder/SubFolder/{1}/{0}.cshtml" };

    public MyViewEngine()
    {
        base.ViewLocationFormats = base.ViewLocationFormats.Union(NewViewFormats).ToArray();
    }
}

我的 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());
    }

我的视图文件夹;

在此处输入图像描述

我的控制器文件夹;

在此处输入图像描述

4

1 回答 1

1

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.

于 2013-03-05T14:54:03.377 回答