【问题标题】:Converted ASP.NET MVC 3 web site is not disaplaying new Razor riews转换后的 ASP.NET MVC 3 网站未显示新的 Razor 视图
【发布时间】:2011-01-24 13:41:22
【问题描述】:

我已将我的 ASP.NET 2010 MVC2 系统更新到 MVC3。我还使用转换工具将一个正在进行的 MVC2 网站更新为 MVC3。我已将当前页面保留为 .aspx,包括母版页。我添加了一个新的 .cshtml 视图。除了新的基于剃刀的视图之外,所有页面都可以正常工作。

我收到以下错误

无法呈现文件“~/Views/Shared/Site.Master”,因为它不存在或不是有效页面。

它确实存在,因为它可以显示 .aspx 页面。如何添加正确的语句以在

中显示 .cshtml 页面
<asp:ContentPlaceHolder ID="mainContent" runat="server">

我试过了

@RenderBody()

这给出了上述错误消息。我也试过了

<%: RenderBody() %>

但这会出错

当前上下文中不存在名称“RenderBody”

堆栈跟踪如下:

   [HttpException (0x80004005): The file "~/Views/Shared/Site.Master" could not be rendered, because it does not exist or is not a valid page.]
   System.Web.WebPages.Util.EnsureValidPageType(WebPageBase page, String virtualPath) +98735
   System.Web.WebPages.WebPageBase.PopContext() +166
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

【问题讨论】:

    标签: .net asp.net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    我认为您不能混合搭配 Razor 页面和 ASP.NET WebForms 母版页。

    见:Is Razor view with ASPX .Master page possible?

    edit:嗯,不是自动的。 http://eworldui.net/blog/post/2011/01/07/Using-Razor-Pages-with-WebForms-Master-Pages.aspx

    【讨论】:

    • 谢谢。我以为我已经检查了所有以前的问题,但我的搜索(MVC3 Razor)没有显示您指向的问题。我现在必须考虑是否将所有内容都切换到 .cshtml 或继续使用 .aspx 页面?
    【解决方案2】:

    有两个文件用于在 ASP.NET MVC 3 应用程序项目中加载 Razor 等效的母版页,它们是

    _ViewStart.cshtml 位于项目的根目录,_Layout.cshtml 文件位于 Views/Shared 文件夹中。 _Layout.cshtml 是您的母版页。代码

    @RenderBody()
    

    类似于 Web 表单母版页中的内容占位符标记。您需要将母版页转换为 razor 语法并将源代码放在 _Layout.cshtml 中。如果您想加载不同的布局视图,您可以将以下代码添加到您的内容视图中。

    @{
       ViewBag.Title = "Content Page";
       Layout = "~/Views/Shared/_MyLayout.cshtml";
    }
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2012-12-02
      • 1970-01-01
      相关资源
      最近更新 更多