asp.net MVC 3,主题(不同意见) [英] asp.net mvc 3, Themes (different Views)

查看:136
本文介绍了asp.net MVC 3,主题(不同意见)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的主题添加到我的网站,将使用完全不同的看法。我希望看到他们在我的项目是这样的:

 查看/ THEME1 / ...
查看/ THEME2 / ...

而不是默认的

 查看/ ...

和表兄弟姐妹中,我需要一个简单的方法在它们之间进行切换。

所以,问题是:我怎样才能使视图引擎去寻找所提到的,例如在一个特定的地方查看,在web.config中

添加

解决这个基本问题,THX到阿尔奇尔:

 公共类ThemedRazorViewEngine:RazorViewEngine
{
    公共ThemedRazorViewEngine(字符串THEMENAME)
    {
        MasterLocationFormats =新的字符串[] {〜/查看/+ THEMENAME +/Shared/{0}.cshtml};
        PartialViewLocationFormats =新的字符串[] {〜/查看/+ THEMENAME +/{1}/{0}.cshtml};
        ViewLocationFormats =新的字符串[] {〜/查看/+ THEMENAME +/{1}/{0}.cshtml};
    }
}

一切都不错,但右键 - >去查看。不工作了(副作用,没什么大不了的)

现在我想到了另一个问题:在网站上,我们有管理面板,应该是独立的主题OFC。我该如何解决呢?有这样的事情:

 查看/管理/ ...
查看/ THEME1 / ...
查看/ THEME2 / ...


在web.config中的主题价值解决方案

简单的方法是创建自定义视图引擎并重写只能查看搜索位置。你不要这样,在控制器的任何改变(例如只使用CSHTML文件C#仅视图引擎,你如果要使用Visual Basic的观点补充vbhtml扩展)

 公共类ThemedRazorViewEngine:RazorViewEngine
{
    公共ThemedRazorViewEngine(字符串THEMENAME)
    {
        AreaMasterLocationFormats =新的字符串[] {〜/地区/ {2} /查看/+ THEMENAME +/{1}/{0}.cshtml,〜/地区/ {2} /查看/共享/+ THEMENAME +/{0}.cshtml};        //和同为以下所有
        AreaPartialViewLocationFormats =新的字符串[] {〜/地区/ {2} /查看/ {1} / {0} .cshtml,〜/地区/ {2} /查看/共享/ {0} .cshtml};
        AreaViewLocationFormats =新的字符串[] {〜/地区/ {2} /查看/ {1} / {0} .cshtml,〜/地区/ {2} /查看/共享/ {0} .cshtml};
        FileExtensions =新的字符串[] {CSHTML};
        MasterLocationFormats =新的字符串[] {〜/查看/ {1} / {0} .cshtml,〜/查看/共享/ {0} .cshtml};
        PartialViewLocationFormats =新的字符串[] {〜/查看/ {1} / {0} .cshtml,〜/查看/共享/ {0} .cshtml};
        ViewLocationFormats =新的字符串[] {〜/查看/ {1} / {0} .cshtml,〜/查看/共享/ {0} .cshtml};
    }
}

和视图引擎登记

  ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(新ThemedRazorViewEngine(ConfigurationManager.AppSettings [currentTheme]));

I want to add themes to my site that will use totally different Views. I want to see them in my project like this:

Views/Theme1/...
Views/Theme2/...

instead of default

Views/...

And of coz I need a simple way to switch between them.

So the question is: how can I make ViewEngine to look for Views in a specific place mentioned, for example, in web.config?

Added

Solved the base problem with this, thx to Archil:

public class ThemedRazorViewEngine : RazorViewEngine
{
    public ThemedRazorViewEngine(string themeName)
    {
        MasterLocationFormats = new string[] { "~/Views/" + themeName + "/Shared/{0}.cshtml" };
        PartialViewLocationFormats = new string[] { "~/Views/" + themeName + "/{1}/{0}.cshtml" };
        ViewLocationFormats = new string[] { "~/Views/" + themeName + "/{1}/{0}.cshtml" };
    }
}

Everything good, but "right click - > go to view" doesn't work any more (side effect, no big deal).

And now I came up with another question: on web site we have administration panel that should be theme independent ofc. How can I fix that? to have something like this:

Views/Admin/...
Views/Theme1/...
Views/Theme2/...

解决方案

Simplest solution for theme value in web.config is to create custom ViewEngine and override only view search locations. This way you dont have to change anything in Controller (Example uses only cshtml files for c# only view engine, you have to add vbhtml extensions if you want to use visual basic views)

public class ThemedRazorViewEngine : RazorViewEngine
{
    public ThemedRazorViewEngine(string themeName)
    {
        AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/ " + themeName + "/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/" + themeName + "/{0}.cshtml" };

        //and same for all of below
        AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" };
        AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" };
        FileExtensions = new string[] { "cshtml" };
        MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
        PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
        ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
    }
}

And registration of view engine

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ThemedRazorViewEngine(ConfigurationManager.AppSettings["currentTheme"]));

这篇关于asp.net MVC 3,主题(不同意见)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆