混合Razor视图和Web表单母版页 [英] Mixing Razor Views and Web Forms Master Pages

查看:166
本文介绍了混合Razor视图和Web表单母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做斯科特·汉塞尔曼(Scott Hanselman)在下面描述的事情:

I'm attempting to do what Scott Hanselman describes below:

http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

我有一个Web窗体用户控件,其背后的代码是从不能在Razor布局中工作的Web窗体和Razor母版页调用的:

I have a Web Form user control with code behind that is called from the Web Form and Razor Master Pages that is not working in the Razor layout:

<div id="navtop">
    @{ Html.RenderPartial("~/Controls/MasterPageMenu.ascx"); }
</div>

用户控件包含以下内容:

The user control contains the following:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MasterPageMenu.ascx.cs" Inherits="Controls.MasterPageMenu" %>

我得到了错误: 位于〜/Controls/MasterPageMenu.ascx"的视图必须派生自ViewPage,ViewPage,ViewUserControl或ViewUserControl.

I get the error: The view at '~/Controls/MasterPageMenu.ascx' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.

让它在Razor视图世界中起作用我错过了什么?

What have I missed in getting this to work in the Razor view world?

推荐答案

这应该有效,只需确保您的部分内容来自ViewUserControl:

This should work, just make sure that your partial derives from ViewUserControl:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl" 
%>

请记住,即使对于WebForms视图引擎,也不建议在ASP.NET MVC中使用服务器端控件,甚至也不应该尝试这样做.在ASP.NET MVC中,您具有布局(如果使用WebForms视图引擎,则为母版页),视图和局部视图.就这样.用户控件属于经典WebForms,而不属于ASP.NET MVC.

Remember that using server side controls in ASP.NET MVC is not something that's recommended even with the WebForms view engine and not something that you should even be attempting to do. In ASP.NET MVC you have layouts (master pages if you use WebForms view engine), views and partials. That's all. User controls belong to classic WebForms, not to ASP.NET MVC.

这篇关于混合Razor视图和Web表单母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆