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

前端 未结 3 2029
天涯浪人
天涯浪人 2021-02-04 05:07

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 a

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 05:38

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

    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

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

    Welcome Test User Control

    3rd Call user control in MVC View 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.

提交回复
热议问题