简体   繁体   English

将路由绑定到MVC4中的自定义物理路径

[英]Bind route to custom physical path in mvc4

I want to set up the routes to open views depend on the custom parameter "theme", like this: 我想根据自定义参数“主题”设置打开视图的路线,如下所示:

http://localhost/black/home   to open  ~/themes/black/views/home/index.cshtml
http://localhost/white/home   to open  ~/themes/white/views/home/index.cshtml

the name of theme is dynamic, and I registered routes like this: 主题名称是动态的,我注册了这样的路线:

    routes.MapRoute("ThemeRoute", "{theme}/{controller}/{action}/{id}"
        , new
        {
            theme = "default",
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });

but it doesn't work. 但这不起作用。 How could I bind the custom route "{theme}/{controller}/{action}/{id}" to the custom physical path like "~/themes/{theme}/views/{view}/{action}"? 如何将自定义路由“ {theme} / {controller} / {action} / {id}”绑定到自定义物理路径,例如“〜/ themes / {theme} / views / {view} / {action}”? or impossible? 还是不可能? I'd appreciate if anyone could give me advices. 如果有人可以给我建议,我将不胜感激。

Update: I decompiled the class System.Web.Mvc.RazorViewEngine and find out how to define my own custom physical path, like this: 更新:我反编译了System.Web.Mvc.RazorViewEngine类,并了解如何定义自己的自定义物理路径,如下所示:

  public sealed class ThemeViewEngine : RazorViewEngine
  { 
        private ThemeViewEngine(IViewPageActivator viewPageActivator)
            : base(viewPageActivator)
        {
            //...
            AreaViewLocationFormats = new[]
            {
                "~/Areas/{2}/Themes/{3}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Themes/{3}/Views/Shared/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml"
            };
            //...
        }
  }

then override the method FindView and added the engine to ViewEngines.Engines in Global.asax, now the ViewEngine can find view pages in my defined paths.But the route still don't accept the url {theme}/{controller}/{action}/{id} ,it seems that the route can't recognize the {theme} is the same thing with what has the same name in the engine. 然后重写方法FindView并将引擎添加到Global.asax的ViewEngines.Engines中,现在ViewEngine可以在我定义的路径中查找视图页面了。但是该路线仍然不接受网址{theme}/{controller}/{action}/{id} ,似乎路由无法识别{theme}是具有相同名称的引擎。 so I use the query string and cookie to control the theme for now,like this: 所以我现在使用查询字符串和cookie来控制主题,如下所示:

http://localhost/home?theme=black   to open  ~/themes/black/views/home/index.cshtml
http://localhost/home?theme=white   to open  ~/themes/white/views/home/index.cshtml

but it's not prefect, I still need help or find the way by myself. 但这不是完美的,我仍然需要帮助或自己寻找方法。

How about using Areas to organized your two themes. 如何使用Areas组织两个主题。 You can have two areas: "Black" and "White". 您可以有两个区域:“黑色”和“白色”。 Each of them has its own controllers and actions. 他们每个人都有自己的控制器和动作。 The route pattern would be: {AreaName}/{Controller}/{Action}/{Id}. 路由模式为:{AreaName} / {Controller} / {Action} / {Id}。 Check this link for how to use areas. 检查此链接以了解如何使用区域。 http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM