asp.net-mvc 在 MVC Razor 视图中使用旧版 ASP.NET ASCX 用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7934319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:32:41  来源:igfitidea点击:

Use a legacy ASP.NET ASCX User Control in MVC Razor view

asp.net-mvcwebformsascx

提问by Walter

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

我正在尝试实现一个使用 WebForm ascx 用户控件(非 MVC)的 MVC Razor _Layout.cshtml 页面。我是根据 Scott Hansleman 文章“Mixing Razor Views and WebForms Master Pages with ASP.NET MVC 3”的“Yes”部分来做这件事的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.

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

From what I've read elsewhere on Stackoverflowit 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:

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

@{ 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?

我在这里缺少什么?

回答by Daniel Schilling

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

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

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

... to this:

...到这个:

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

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

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

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

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

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

'controlType' 类型的控件 'controlId' 必须放置在带有 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.

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

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 andpost-back handling logic, into something that just renders a view.

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

You canmake one ascxcontrol 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 ascxuser control and an MVC Razor view.

可以使一个ascx控件同时与 WebForms 和 Razor MVC 配合使用,对于页眉和页脚或只读数据视图等简单事物,这是将应用程序迁移到 MVC 的好方法。但是对于更复杂的东西,比如输入表单,维护ascx用户控件和 MVC Razor 视图可能会更容易。

回答by adnan

you can use asp.net user control in MVC by the following way

您可以通过以下方式在MVC中使用asp.net用户控件

1st change user control inheritance this as by default

第一个更改用户控件继承默认为

default:

默认:

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

change:

改变:

public partial class TestControl: ViewUserControl
{
// ...
}`

2nd user control with asp.net controls like this one within form tag and script manager should be call

第二个用户控件与 asp.net 控件中的表单标记和脚本管理器中的这样的控件应该被调用

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestApp.UserControls.TestControl" %>

<div>
<h1>Welcome Test User Control</h1>
<form id="test" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:TextBox runat="server" ID="txtbxTest" Text="Hello">

</asp:TextBox>
</form>
</div>

3rd Call user control in MVC View as

MVC 视图中的 3rd Call user control as

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

it should work without any issue, and it will be beneficial to convert old asp.net website into MVC, if old website have user controls, its so easy to upgrade with new technology in MVC.

它应该可以正常工作,并且将旧的asp.net网站转换为MVC将是有益的,如果旧网站有用户控件,那么在MVC中使用新技术升级很容易。

回答by SLaks

You cannot use classic ASP.Net usercontrols (w/ postbacks and ViewState) in ASP.Net MVC.

您不能在 ASP.Net MVC 中使用经典的 ASP.Net 用户控件(带回发和 ViewState)。

Html.Partialonly allows you to render partial views, which happen to use the same extension, but are not the same.

Html.Partial只允许你渲染部分视图,这些视图碰巧使用相同的扩展名,但不一样。