Mixing Razor Views and Web Forms Master Pages

允我心安 提交于 2020-01-04 07:54:09

问题


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?


回答1:


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

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.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.



来源:https://stackoverflow.com/questions/7407801/mixing-razor-views-and-web-forms-master-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!