如何正确获取嵌套文件夹以在 MVC 5 C# 中呈现

How to Properly Get Nested Folders to Render in MVC 5 C#

我得到了所有 MVC 5 都按预期工作的传统网站,一切都正确呈现并且工作正常。

我已经添加了一个仪表板,我正在尝试将此仪表板设置为文件夹结构,使其与 MVC 5 C# 网站的其余部分隔离;但是,我收到一个服务器错误,指出布局引擎无法找到我的局部视图。

现在,当视图本身加载时它通过了测试并且确实返回了正确的视图,当布局引擎无法定位嵌套的分部视图时就会出现问题。

由于我对 MVC 5 没有完全的了解,所以我在这方面迷失了方向。

现在,就像之前提到的,关于开箱即用的文件夹结构,一切都完美无缺,但是当我创建嵌套文件夹并放置部分视图时,MVC 5 引擎无法找到这些嵌套的部分视图。

嵌套这些部分视图的原因很明显,我将实际的传统网站与用户仪表板视图分开。

当我单击指向索引文件的操作 link 时,这有效,但是当主体视图试图渲染时,它找不到嵌套的局部视图。

我在这里错过了什么?我希望我提供的信息足够多,可以尽可能轻松地回答这个问题。

加载实际用户仪表板索引有效

 @{
    ViewBag.Title = "Your Dashboard";
    Layout = "~/Views/Shared/UserDashboard/_Layout_Dashboard.cshtml";
}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

加载实际的部分是服务器说不不,你做的不正确。

这是确切的服务器错误,请告诉我正确的实现方法。

Server Error in '/' Application.
The partial view '~/Views/Shared/UserDashboard/_PartialHeader_Dashboard' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Shared/UserDashboard/_PartialHeader_Dashboard
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The partial view '~/Views/Shared/UserDashboard/_PartialHeader_Dashboard' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Shared/UserDashboard/_PartialHeader_Dashboard

Source Error:


Line 26: <script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>*@
Line 27: <body>
Line 28:     @Html.Partial("~/Views/Shared/UserDashboard/_PartialHeader_Dashboard")
Line 29:     @Html.Partial("~/Views/Shared/UserDashboard/_PartialLeftNav_Dashboard")
Line 30:     @RenderBody()


Source File: D:\Users\Erik\Documents\visual studio 2017\Projects\BuilderBits\BuilderBits\Views\Shared\UserDashboard\_Layout_Dashboard.cshtml    Line: 28

Stack Trace:


[InvalidOperationException: The partial view '~/Views/Shared/UserDashboard/_PartialHeader_Dashboard' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Shared/UserDashboard/_PartialHeader_Dashboard]
   System.Web.Mvc.HtmlHelper.FindPartialView(ViewContext viewContext, String partialViewName, ViewEngineCollection viewEngineCollection) +327
   System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +264
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +91
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName) +32
   ASP._Page_Views_Shared_UserDashboard__Layout_Dashboard_cshtml.Execute() in D:\Users\Erik\Documents\visual studio 2017\Projects\BuilderBits\BuilderBits\Views\Shared\UserDashboard\_Layout_Dashboard.cshtml:28
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +90
   System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) +232
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +80
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +63
   System.Web.WebPages.WebPageBase.PopContext() +237
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9765045
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

当您尝试使用 @Html.Partial("_PartialHeader_Dashboard") 包含局部视图时,MVC 框架将尝试根据 MVC 约定从以下位置加载局部视图。

  1. ~/Views/DirectoryWithYourCurrentControllerName/
  2. ~/Views/Shared/

框架不知道您的局部视图在子目录中,除非您通过扩展视图引擎和指定视图文件位置告诉框架这样做(ViewLocationFormats)。您需要做的就是添加您的自定义位置。

您有 2 个选择

用目录名调用局部视图(在shared下)

@Html.Partial("UserDashboard/_PartialHeader_Dashboard")

使用完整文件路径

@Html.Partial("~/Views/Shared/UserDashboard/_PartialHeader_Dashboard.cshtml")

但更好的选择是考虑 Areas。区域用于在逻辑上将事物(控制器/视图等)保持在一起。