如何获取高级URL路由参数?

时间:2013-03-05 13:20:32

标签: asp.net-mvc url-routing viewengine

现在可以像 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());
    }

我的观点文件夹;

enter image description here

我的控制器文件夹;

enter image description here

1 个答案:

答案 0 :(得分:1)

MVC路由不像旧式ASP.NET站点那样基于文件系统。所有控制器都在编译时注册,路由只选择具有匹配名称的控制器。控制器存在于文件系统中的地方无关紧要。