首页 > 解决方案 > 如何修复 Sitecore 9.3 中的错误渲染视图?

问题描述

我目前正在学习 Sitecore 的课程,但现在我被困在一个我找不到的命名空间上。我尝试将其添加到 /Views 和 Project 文件夹中的 Web.cfg 中。每次我尝试打开体验编辑器时,都会出现以下错误:

Error Rendering View: /Views/ClothingCo/Layouts/default.cshtml: Error while rendering view: '/Views/ClothingCo/Layouts/default.cshtml' (model: 'Sitecore.Mvc.Presentation.RenderingModel, Sitecore.Mvc').
   at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer)
   at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)
Inner Exception: c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET     Files\root\16344e6a\1fed4132\App_Web_default.cshtml.25717ab2.1h7yetdm.0.cs(39): error CS0246: The     type or namespace name 'BasicCompany' could not be found (are you missing a using directive or an     assembly reference?)
   at System.Web.Compilation.BuildManager.PostProcessFoundBuildResult(BuildResult result,     Boolean keyFromVPP, VirtualPath virtualPath)
   at System.Web.Compilation.BuildManager.GetBuildResultFromCacheInternal(String cacheKey,     Boolean keyFromVPP, VirtualPath virtualPath, Int64 hashCode, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath     virtualPath, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath,     Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound,     Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context,     VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile,     Boolean throwIfNotFound, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath,     HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
   at System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String     partialViewName, Object model, ViewDataDictionary viewData)
   at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer)

我的 default.cshtml 文件如下所示:

@using Sitecore.Mvc @using Sitecore.Mvc.Analytics.Extensions @using Sitecore.Mvc.Presentation @model RenderingModel @{ Layout = null; }
<!DOCTYPE html>
<html>

<head>
  <title>@Html.Sitecore().Field("Page Title", new { DisableWebEdit = true })</title>
  @Html.Sitecore().VisitorIdentification()
</head>

<body>
  <h1>@Html.Sitecore().Field("Page Title")</h1>
  <div>
    @Html.Sitecore().Placeholder("main")
  </div>
</body>

</html>

标签: asp.netsitecoresitecore-mvcsitecore9

解决方案


确保渲染项(/sitecore/layout/Renderings/Your View rendering Item)正确填写了这两个字段:

  • 路径:(路径/to/your/view.cshtml)
  • 模型:(路径/到/您的/模型项/in/SitecoreTree)
    • 应该在那里:/sitecore/layout/Models

然后,确保您的模型项的字段模型类型也正确填写

  • namespace.yourclass,程序集

还,

  • 确保您的 default.cshtml 可以在您的 inetpub 中找到:C:\inetpub\wwwroot\yourSite\Website\Views\ClothingCo\Layouts\default.cshtml
  • 由于错误指出了这一点:找不到类型或名称空间名称“BasicCompany”,这让我质疑您的 cshtml 有效性。也许您没有编辑好的那个,因为它应该在您的视图中作为模型类型的 using 语句,或者直接在模型中。但是根据您的 default.cshtml 示例,没有对此模型的引用。也许您正在编辑或查看错误的地方。
  • 您是否还可以确保您尝试查看的项目具有正确设置的演示详细信息?
    • 在内容编辑器上,单击您的项目。
    • 在导航中(具有从左到右具有这些选项卡的灰色背景的导航:主页、导航、审阅……)单击演示选项卡。
    • 现在,在下面的导航中(白色背景的导航,从左到右有这些选项卡:保存、详细信息、重置……)单击详细信息。
    • 确保正在使用的布局是好的布局(应该是保存路径的布局项:/Views/ClothingCo/Layouts/default.cshtml)。
  • 确保在控件中添加的渲染也是有效的。

推荐阅读