它现在像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());
}
我的视图文件夹;
我的控制器文件夹;