混合 Razor 视图和 Web 表单母版页
我正在尝试执行 Scott Hanselman 下面描述的操作:
http://www.hanselman.com/blog /MixinRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
我有一个 Web 窗体用户控件,其背后的代码是从Web 表单和 Razor 母版页在 Razor 布局中不起作用:
<div id="navtop">
@{ Html.RenderPartial("~/Controls/MasterPageMenu.ascx"); }
</div>
用户控件包含以下内容:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MasterPageMenu.ascx.cs" Inherits="Controls.MasterPageMenu" %>
我收到错误: “~/Controls/MasterPageMenu.ascx”中的视图必须派生自 ViewPage、ViewPage、ViewUserControl 或 ViewUserControl。
在让它在 Razor 视图世界中工作时我错过了什么?
I'm attempting to do what Scott Hanselman describes below:
http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
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" %>
I get the error:
The view at '~/Controls/MasterPageMenu.ascx' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.
What have I missed in getting this to work in the Razor view world?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可行,只需确保您的部分派生自 ViewUserControl:
请记住,即使使用 WebForms 视图引擎,也不推荐在 ASP.NET MVC 中使用服务器端控件,您甚至不应该尝试这样做。在 ASP.NET MVC 中,您有布局(如果您使用 WebForms 视图引擎,则为母版页)、视图和部分。就这样。用户控件属于经典 WebForms,而不属于 ASP.NET MVC。
This should work, just make sure that your partial derives from ViewUserControl:
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.