简体   繁体   English

在MVC中清除视图引擎会破坏站点地图的面包屑样式

[英]Clearing View Engines in MVC breaks sitemap breadcrumb styles

In a MVC 5 web application that i am developing, for performance sake i am clearing the view engines and adding only the RazorViewEngine in Global.asax.cs using ViewEngines.Engines.Clear(); 在我正在开发一个MVC 5 Web应用程序,为性能的缘故,我正在清除视图引擎并添加只有RazorViewEngineGlobal.asax.cs使用ViewEngines.Engines.Clear(); and it breaks the styles in the mvc sitemap breadcrumbs 它打破了mvc sitemap面包屑中的样式

Before 之前

在此输入图像描述

After

在此输入图像描述

My sitemap partial view (SiteMapPathHelperModel.cshtml) looks like 我的站点地图局部视图(SiteMapPathHelperModel.cshtml)看起来像

@model MvcSiteMapProvider.Web.Html.Models.SiteMapPathHelperModel
@using System.Web.Mvc.Html
@using System.Linq
@using MvcSiteMapProvider.Web.Html.Models
<ol class="breadcrumb">
    @foreach (var node in Model)
    {
        if (node == Model.Last())
        {
            <li class="active">
                <strong>@Html.DisplayFor(m => node)</strong>
            </li>
        }
        else
        {
            <li>
                @if (node.Title == "Home")
                {
                    <a href="/"><i class="fa fa-lg fa-home"></i></a>

                }
                else
                {
                    @Html.DisplayFor(m => node)
                }
            </li>
        }
    }
</ol>

Somehow it appears to be falling back to another implementation than the partials under views > shared > display templates 不知何故,它似​​乎回落到另一个实现,而不是视图>共享>显示模板下的部分

What could be the cause? 可能是什么原因?

Edit 编辑

I've missed some vital info here, another dev has done this at the bottom of the Application_Start() 我在这里错过了一些重要的信息,另一个开发人员在Application_Start()的底部完成了这个

ViewEngines.Engines.Add(new RazorViewEngine
{
     PartialViewLocationFormats = new string[]
     {
           "~/Areas/Shared/{0}.cshtml",
     }
});

I'm not seeing an issue with the information you provided, but my guess is that you are not clearing and setting the view engines inside of the Application_Start method. 我没有看到您提供的信息存在问题,但我的猜测是您没有清除并在Application_Start方法中设置视图引擎。

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new RazorViewEngine());

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

Note that the internal MvcSiteMapProvider engine is for backup when there are no views in your /Views/Shared/DisplayTemplates/ folder. 请注意,当/Views/Shared/DisplayTemplates/文件夹中没有视图时,内部MvcSiteMapProvider引擎用于备份。 It will serve the default template from within the DLL in this case, which is what appears to be happening here. 在这种情况下,它将从DLL中提供默认模板,这似乎是在这里发生的。

That could happen if your Razor view engine is registered after the internal view engine. 如果您的Razor视图引擎在内部视图引擎之后注册,则可能发生这种情况。 So, if you don't have this code in Application_Start (as you should), then you could try to insert the Razor view engine to ensure it is placed before the MvcSiteMapProvider fallback engine. 因此,如果您在Application_Start没有此代码(如您MvcSiteMapProvider ),那么您可以尝试插入Razor视图引擎以确保它放在MvcSiteMapProvider回退引擎之前。

ViewEngines.Engines.Clear();
ViewEngines.Engines.Insert(0, new RazorViewEngine());

Or as already mentioned, just remove the web page view engine in a single action. 或者如前所述,只需在一个操作中删除网页视图引擎即可。

ViewEngines.Engines.RemoveAt(0);

The internal view engine is registered the first time a MvcSiteMapProvider HTML helper is called. 第一次调用MvcSiteMapProvider HTML帮助程序时注册内部视图引擎。 So if you are calling the add method after that point, you are adding the Razor view engine after the internal MvcSiteMapProvider view engine. 因此,如果您在该点之后调用add方法,则在内部MvcSiteMapProvider视图引擎之后添加Razor视图引擎。

When the MvcSiteMapProvider is loaded, it uses its own ViewEngine. 加载MvcSiteMapProvider ,它使用自己的ViewEngine。 I suspect you are removing it. 我怀疑你要删除它。

Try adding a new MvcSiteMapProvider.Web.Mvc.MvcSiteMapProviderViewEngine() object to the list of ViewEngines. 尝试将新的MvcSiteMapProvider.Web.Mvc.MvcSiteMapProviderViewEngine()对象添加到MvcSiteMapProvider.Web.Mvc.MvcSiteMapProviderViewEngine()列表中。

Thanks NightOwl and Rowan 谢谢NightOwl和Rowan

The solution was to add "~/Views/Shared/{0}.cshtml" as an item in PartialViewLocationFormats 解决方案是在PartialViewLocationFormats添加"~/Views/Shared/{0}.cshtml"作为项目

  ViewEngines.Engines.Add(new RazorViewEngine
            {
                PartialViewLocationFormats = new string[]
                {
                    "~/Areas/Shared/{0}.cshtml",
                    "~/Views/{1}/{0}.cshtml"
                    "~/Views/Shared/{0}.cshtml",
                }
            });

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

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