C# 将路由绑定到mvc4中的自定义物理路径

C# 将路由绑定到mvc4中的自定义物理路径,c#,asp.net-mvc,asp.net-mvc-4,routes,C#,Asp.net Mvc,Asp.net Mvc 4,Routes,我想根据自定义参数主题设置打开视图的路线, 像这样: http://localhost/black/home to open ~/themes/black/views/home/index.cshtml http://localhost/white/home to open ~/themes/white/views/home/index.cshtml routes.MapRoute("ThemeRoute", "{theme}/{controller}/{action}/{

我想根据自定义参数主题设置打开视图的路线, 像这样:

http://localhost/black/home   to open  ~/themes/black/views/home/index.cshtml
http://localhost/white/home   to open  ~/themes/white/views/home/index.cshtml
    routes.MapRoute("ThemeRoute", "{theme}/{controller}/{action}/{id}"
        , new
        {
            theme = "default",
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });
  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"
            };
            //...
        }
  }
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
主题的名称是动态的,我注册了如下路线:

http://localhost/black/home   to open  ~/themes/black/views/home/index.cshtml
http://localhost/white/home   to open  ~/themes/white/views/home/index.cshtml
    routes.MapRoute("ThemeRoute", "{theme}/{controller}/{action}/{id}"
        , new
        {
            theme = "default",
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });
  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"
            };
            //...
        }
  }
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
但它不起作用。如何将自定义路由{theme}/{controller}/{action}/{id}绑定到~/themes/{theme}/views/{view}/{action}这样的自定义物理路径?还是不可能? 如果有人能给我建议,我将不胜感激

更新: 我反编译了类System.Web.Mvc.RazorViewEngine,了解如何定义自己的自定义物理路径,如下所示:

http://localhost/black/home   to open  ~/themes/black/views/home/index.cshtml
http://localhost/white/home   to open  ~/themes/white/views/home/index.cshtml
    routes.MapRoute("ThemeRoute", "{theme}/{controller}/{action}/{id}"
        , new
        {
            theme = "default",
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });
  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"
            };
            //...
        }
  }
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
然后重写FindView方法并将引擎添加到ViewEngines.Engines的Global.asax中,现在ViewEngine可以在我定义的路径中找到视图页面。但是路由仍然不接受url{theme}/{controller}/{action}/{id},似乎路由无法识别{theme}与引擎中具有相同名称的内容相同。现在我使用查询字符串和cookie来控制主题,如下所示:

http://localhost/black/home   to open  ~/themes/black/views/home/index.cshtml
http://localhost/white/home   to open  ~/themes/white/views/home/index.cshtml
    routes.MapRoute("ThemeRoute", "{theme}/{controller}/{action}/{id}"
        , new
        {
            theme = "default",
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });
  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"
            };
            //...
        }
  }
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

但这并不完美,我仍然需要帮助或自己找到方法。

如何使用区域来组织您的两个主题。您可以有两个区域:黑色和白色。它们每个都有自己的控制器和操作。 路由模式将是:{AreaName}/{Controller}/{Action}/{Id}。 查看此链接了解如何使用区域。

谢谢郭军,但恐怕我不能使用区域来进行此操作,因为如果我想同时使用主题和区域,这将再次成为一个问题。