ASP.NET MVC 1 的主题支持,获取视图大师名称

发布于 2024-10-27 09:14:34 字数 553 浏览 1 评论 0原文

目前我正在开发一个较旧的 ASP.NET MVC 1 应用程序,以添加主题支持。我浏览了网络并能够构建自己的 ViewEngine,到目前为止它运行得很好。只有一个问题是敲打我的。

我已经为 WebFormViewEngine 重写了以下方法:

public override ViewEngineResult FindView(
    ControllerContext controllerContext, 
    string viewName, 
    string masterName, 
    bool useCache)

在该方法中,我正在设置主题支持的位置格式。不幸的是,masterName 参数始终为空!所以我需要

if (string.IsNullOrEmpty(masterName))    
    masterName = "Site";

经常亲自检查以使发动机正常工作。但由于我有几个主文件,一旦视图需要另一个主文件而不是“站点”,这个解决方案就很糟糕。 有谁知道,我如何在此方法中获取主视图名称?

currently I'm developing on an older ASP.NET MVC 1 Application, to add theme support. I've looked around the web and was able to build my own ViewEngine which works quit nice so far. Only one problem is banging my had.

I've overwritten following method for WebFormViewEngine:

public override ViewEngineResult FindView(
    ControllerContext controllerContext, 
    string viewName, 
    string masterName, 
    bool useCache)

In this method I'm setting up the location formats for the theme support. Unfortunately the masterName Parameter is always empty! So I need to check

if (string.IsNullOrEmpty(masterName))    
    masterName = "Site";

always by myself to get the engine working. But as I've several master files, this solution sucks, as soon as a view requires another master than "Site".
Does anybody know, how I can get the master views name in this Method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨与橙与城 2024-11-03 09:14:34

自己解决了。经过大量研究,我发现了以下片段,这对我有帮助:

private void RenderViewPage(ViewContext context, ViewPage page)
            {
                if (!String.IsNullOrEmpty(MasterPath)) {
                    page.MasterLocation = MasterPath;
                } else {
                    if (sc.WIP.CarharttIMD.Common.Config.GetStringValue("Theme") != "Default")
                        page.PreInit += new EventHandler(page_PreInit);
                }

                page.ViewData = context.ViewData;
                page.RenderView(context);
            }

void page_PreInit(object sender, EventArgs e)
            {
                ViewPage page = sender as ViewPage;
                //test for Default theme path, and replace with current theme
                string defaultthemepath = string.Format("{0}Content/Default", page.Request.ApplicationPath);
                if (!string.IsNullOrEmpty(page.MasterPageFile) && !page.MasterPageFile.ToLower().StartsWith(defaultthemepath.ToLower()))
                {
                    string masterPagePath = page.MasterPageFile;
                    int lastIndexOfSlash = masterPagePath.LastIndexOf('/');
                    string masterPageName = masterPagePath.Substring(lastIndexOfSlash + 1, masterPagePath.Length - lastIndexOfSlash - 1);
                    string newMaster = string.Format(
                        "~/Content/{0}/Views/Shared/{1}",
                        Common.Config.GetStringValue("Theme"),
                        masterPageName
                    );
                    if (File.Exists(page.Server.MapPath(newMaster)))
                        page.MasterLocation = newMaster;
                }
            }

必须子类化 WebViewForm 并在 PreInit 事件中处理主文件。

Solved it by myself. After a lot of researching, I've found following snippet, which helped me:

private void RenderViewPage(ViewContext context, ViewPage page)
            {
                if (!String.IsNullOrEmpty(MasterPath)) {
                    page.MasterLocation = MasterPath;
                } else {
                    if (sc.WIP.CarharttIMD.Common.Config.GetStringValue("Theme") != "Default")
                        page.PreInit += new EventHandler(page_PreInit);
                }

                page.ViewData = context.ViewData;
                page.RenderView(context);
            }

void page_PreInit(object sender, EventArgs e)
            {
                ViewPage page = sender as ViewPage;
                //test for Default theme path, and replace with current theme
                string defaultthemepath = string.Format("{0}Content/Default", page.Request.ApplicationPath);
                if (!string.IsNullOrEmpty(page.MasterPageFile) && !page.MasterPageFile.ToLower().StartsWith(defaultthemepath.ToLower()))
                {
                    string masterPagePath = page.MasterPageFile;
                    int lastIndexOfSlash = masterPagePath.LastIndexOf('/');
                    string masterPageName = masterPagePath.Substring(lastIndexOfSlash + 1, masterPagePath.Length - lastIndexOfSlash - 1);
                    string newMaster = string.Format(
                        "~/Content/{0}/Views/Shared/{1}",
                        Common.Config.GetStringValue("Theme"),
                        masterPageName
                    );
                    if (File.Exists(page.Server.MapPath(newMaster)))
                        page.MasterLocation = newMaster;
                }
            }

Had to subclass the WebViewForm and handeling the master File in the PreInit event.

你的心境我的脸 2024-11-03 09:14:34

解决了类似的问题,但方法略有不同。

假设您在 Theme 文件夹中有替代视图树,那么您必须在从 WebFormViewEngine 派生的类 MyViewEngine 中进行设置:

base.MasterLocationFormats =  new[] {
                        "~/Theme/Views/{1}/{0}.master", 
                        "~/Theme/Views/Shared/{0}.master"
                    }
                ).ToArray();

base.ViewLocationFormats = viewLocationFormats.Concat(
                    new[] {
                        "~/Theme/Views/{1}/{0}.aspx", 
                        "~/Theme/Views/Shared/{0}.aspx", 
                    }
                ).ToArray()

并覆盖方法:

protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
    return System.IO.File.Exists(controllerContext.HttpContext.Server.MapPath(virtualPath));
}

在 Global.asax.cs 文件中的 Application_Start 方法中添加:

System.Web.Mvc.ViewEngines.Engines.Clear();
System.Web.Mvc.ViewEngines.Engines.Add(new WebFormThemeViewEngine());

Solved kind of the same problem but the approach was little different.

Suppose you have alternative views tree in Theme folder then you have to set in your class MyViewEngine derived from WebFormViewEngine:

base.MasterLocationFormats =  new[] {
                        "~/Theme/Views/{1}/{0}.master", 
                        "~/Theme/Views/Shared/{0}.master"
                    }
                ).ToArray();

base.ViewLocationFormats = viewLocationFormats.Concat(
                    new[] {
                        "~/Theme/Views/{1}/{0}.aspx", 
                        "~/Theme/Views/Shared/{0}.aspx", 
                    }
                ).ToArray()

and override method:

protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
    return System.IO.File.Exists(controllerContext.HttpContext.Server.MapPath(virtualPath));
}

In Application_Start method from Global.asax.cs file add:

System.Web.Mvc.ViewEngines.Engines.Clear();
System.Web.Mvc.ViewEngines.Engines.Add(new WebFormThemeViewEngine());
罪#恶を代价 2024-11-03 09:14:34

或者,您可能可以使用我在这个答案中描述的一些技术:如何更改 asp.net mvc 2 中的主题

它适用于 MVC3 和 Razor,但除 View 之外的所有内容都应该在 MVC 1 上正常工作。

Alternatively, you could probably use some of the techniques I've described in this answer: how to change the themes in asp.net mvc 2

It's on MVC3 and Razor, but everything except the View should work just fine on MVC 1 as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文