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

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

问题描述

我正在尝试实现一个使用 WebForm ascx 用户控件(非 MVC)的 MVC Razor _Layout.cshtml 页面.我是根据 Scott Hansleman 文章Mixing Razor Views and WebForms Master Pages with ASP.NET MVC 3"http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

文章说明了如何在 webform Site.Master 页面和 MVC Razor _Layout 页面中使用相同的 ascx 用户控件.

从我在 Stackoverflow 上的其他地方读到的 可以在 MVC 页面中使用旧的 ascx 用户控件(以及 ASP.NET webform 服务器控件).使用以下行应该在我的 Razor _Layout 中呈现 ascx 用户控件:

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

然而,这会引发错误:

'~/UserControls/WebUserControl1.ascx' 处的视图必须从 ViewPage 派生,ViewPage、ViewUserControl 或 ViewUserControl.

我也尝试了以下类似的结果:

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

我在这里遗漏了什么?

解决方案

如错误消息所述,用户控件必须派生自 ... ViewUserControl".在用户控件的代码隐藏文件中,只需更改此...

公共部分类 WebUserControl1 : UserControl{//...}

...到这个:

公共部分类 WebUserControl1 : ViewUserControl{//...}

ViewUserControl 继承自 UserControl,因此它将继续在您现有的 WebForms 页面中工作.

不过,为了让您的用户控制权在 MVC 中工作,您可能需要处理除此之外的其他问题.我遇到的至少另一个是(由 SLaks 暗示):

<块引用>

'controlType' 类型的控件 'controlId' 必须放置在带有 runat=server 的表单标签内.

如果您遇到这样的事情,您将不得不发挥创意.要么修改用户控件,使其可以在 WebForms 和 MVC 中愉快地使用(用通用 HTML 等效项替换有问题的控件 - 包含所有含义),要么复制它,以便您拥有 WebForms 版本和 MVC 版本.

例如,您必须将 <asp:TextBox ID="search"> 替换为 <input type="text" name="search"/>,这意味着您还需要修改处理来自该输入的值的服务器端代码.基本上,您必须对用户控件进行中性处理,将其从用于包含视图呈现逻辑 回发处理逻辑的内容转换为仅呈现视图的内容.

可以使一个 ascx 控件在 WebForms 和 Razor MVC 中都能很好地发挥作用,对于简单的东西,例如页眉和页脚或数据的只读视图,这是将应用程序迁移到 MVC 的好方法.但是对于更复杂的东西,比如输入表单,维护 ascx 用户控件和 MVC Razor 视图可能会更容易.

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

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.

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?

解决方案

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
{
    // ...
}

... to this:

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

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

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):

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

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.

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.

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天全站免登陆