使用MVC的Razor视图遗留ASP.NET ASCX用户控件 [英] Use a legacy ASP.NET ASCX User Control in MVC Razor view

查看:378
本文介绍了使用MVC的Razor视图遗留ASP.NET ASCX用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用一个WebForm ASCX用户控件(非MVC)MVC模式剃刀_Layout.cshtml页面。我这样做基于掀起了是,该斯科特Hansleman一节混合剃刀视图和WebForms的母版页的ASP.NET MVC 3的http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

I am trying to implement a MVC Razor _Layout.cshtml page that uses a WebForm ascx User Control (non-MVC). I am doing this based off the "Yes" section of this Scott Hansleman article "Mixing Razor Views and WebForms Master Pages with ASP.NET MVC 3" http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

文章说如何使用相同的ASCX用户控件在这两个一个WebForm的Site.Master页面以及一个MVC剃刀_layout页。

The article says how to use the same ascx User Control in both a webform Site.Master page as well as an MVC Razor _Layout page.

从我读过<一个href=\"http://stackoverflow.com/questions/6538869/mvc3-razor-is-it-possible-to-render-a-legacy-ascx\">elsewhere #2 该MVC中的网页,可以使用传统的ASCX用户控件(以及ASP.NET Web窗体服务器控件)。使用下面的行应该呈现在我的剃刀_layout的ASCX用户控件:

From what I've read elsewhere on Stackoverflow it is possible to use legacy ascx user controls (as well as ASP.NET webform server controls) in MVC pages. Using the following line should render the ascx user control in my Razor _Layout:

@{ Html.RenderPartial("~/UserControls/WebUserControl1.ascx"); }

不过,这引发错误:

However, that throws the error:

The view at '~/UserControls/WebUserControl1.ascx' must derive from ViewPage,
ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>.

我也曾尝试下面类似的结果:

I have also tried the below with similar results:

@Html.Partial("~/UserControls/WebUserControl1.ascx")

我是什么在这里失踪?

What am I missing here?

推荐答案

随着错误消息状态,用户控制必须从... ViewUserControl 导出。在隐藏文件的用户控件code,只是改变了...

As the error message states, the user control "must derive from ... ViewUserControl". In the code behind file for your user control, simply change this...

public partial class WebUserControl1 : UserControl
{
    // ...
}

...这样:

public partial class WebUserControl1 : ViewUserControl
{
    // ...
}

ViewUserControl 用户控件继承,因此它会继续在现有的WebForms页的工作。

ViewUserControl inherits from UserControl, so it will continue to work in your existing WebForms pages.

您可能不得不处理超出了一个额外的问题,为了让您的用户控件MVC工作,虽然。至少一个其他的,我遇到过(通过 SLaks 提到):

You may have to deal with additional issues beyond this one in order to get your user control to work in MVC, though. At least one other that I encountered is (alluded to by SLaks):

控制控件ID必须放在与RUNAT =服务器的表单标签内。

Control 'controlId' of type 'controlType' must be placed inside a form tag with runat=server.

如果您遇到这样的东西,你将不得不发挥创意。要么修改用户控件,以便它可以在两个的WebForms快乐地生活和MVC(与一般的HTML等值更换有问题的控制 - 与所有的影响),或重复的,所以你有一个WebForms的版本和MVC版本

If you encounter stuff like this, you're going to have to get creative. Either modify the user control so that it can live happily in both WebForms and MVC (replace the offending controls with generic HTML equivalents - with all the implications of that), or duplicate it so you have a WebForms version and an MVC version.

例如,你将不得不更换像&LT的东西; ASP:文本框ID =搜索&GT; &LT;输入类型= 文本NAME =搜索/&GT; ,这意味着你还需要修改服务器端code,处理从输入来的值。基本上,你必须阉割用户控件,从一些用来体现两个视图渲染逻辑转换它的的后回处理逻辑,到的东西,只是呈现一个视图。

For example, you will have to replace things like <asp:TextBox ID="search"> with <input type="text" name="search" />, which means you also need to modify the server-side code that handles the value coming from that input. Basically you have to neuter the user control, converting it from something that used to embody both view rendering logic and post-back handling logic, into something that just renders a view.

可以做一个 ASCX 控制既WebForms和MVC剃刀很好的发挥,像页眉和页脚或简单的事情读只有数据的意见,这是迁移应用程序MVC一个不错的办法。但是,对于像输入形式更复杂的事情,它可能会更容易维护既有 ASCX 的用户控制和一个MVC的Razor视图。

You can make one ascx control play nicely with both WebForms and Razor MVC, and for simple things like page headers and footers or read-only views of data this is a good approach for migrating an application to MVC. But for more complex things like input forms, it will probably be easier to maintain both an ascx user control and an MVC Razor view.

这篇关于使用MVC的Razor视图遗留ASP.NET ASCX用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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