将ASP.NET MVC放入WebForms页面

时间:2022-04-07 05:26:09

Is there any way to render a normal view of asp.net MVC action onto an old .aspx WebForm using an MVC helper or some other method?

有没有办法使用MVC助手或其他方法将asp.net MVC动作的正常视图渲染到旧的.aspx WebForm上?

3 个解决方案

#1


11  

Disclaimer: The proposed solution below is a hack.

免责声明:以下建议的解决方案是黑客攻击。

Suppose that you have the following controller:

假设您有以下控制器:

public class HomeController : Controller
{
    [ChildActionOnly]
    public ActionResult Foo()
    {
        return View();
    }
}

and a corresponding partial (Foo.ascx).

和相应的部分(Foo.ascx)。

And now in a legacy WebForms page you wanted to use this action. Here's how you could proceed:

现在,在遗留的WebForms页面中,您想要使用此操作。以下是您可以继续的方式:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script type="text/C#" runat="server">
    private class DummyController : Controller { }

    public HtmlHelper Html { get; private set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        var httpContext = new HttpContextWrapper(Context);
        var controllerContext = new ControllerContext(
            httpContext, 
            new RouteData(), 
            new DummyController()
        );
        var viewContext = new ViewContext(
            controllerContext, 
            new WebFormView("Views"), 
            new ViewDataDictionary(), 
            new TempDataDictionary(), 
            TextWriter.Null
        );
        Html = new HtmlHelper(viewContext, new ViewPage());  
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%= Html.Action("About", "Home") %>
    </div>
    </form>
</body>
</html>

#2


2  

I used this code in my MasterPage and it worked. But after days I have found that my application was restarting every minute because of memory leaks. I was hunting days for the cause and finnally, this part of code was the cause of memory leak. You have to dispose DummyController and ViewPage. I have fix it like this:

我在我的MasterPage中使用了这段代码并且它有效。但几天后我发现我的应用程序因为内存泄漏而每分钟都重启。我正在寻找原因和最终,这部分代码是内存泄漏的原因。你必须处理DummyController和ViewPage。我这样解决了:

 public class BaseMaster :  System.Web.UI.MasterPage
 {

#region MVC
    Controller _controller= new DummyController();
    ViewPage _viewPage = new ViewPage();

    private class DummyController : Controller { }

    public HtmlHelper Html { get; private set; }

    private void InitHtmlHelper()
    {          
        var httpContext = new HttpContextWrapper(Context);
        var controllerContext = new ControllerContext(
            httpContext,
            new RouteData(),
            _controller
        );
        var viewContext = new ViewContext(
            controllerContext,
            new WebFormView(controllerContext,"Views"),
            new ViewDataDictionary(),
            new TempDataDictionary(),
            TextWriter.Null
        );
        Html = new HtmlHelper(viewContext, _viewPage);  
    }


    public override void Dispose()
    {
        base.Dispose();

        _controller.Dispose();
        _viewPage.Dispose();
    }

#endregion


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            InitHtmlHelper();
            ...
            ...
        }

        ...
   }

#3


0  

I think the easiest would be using client-side scripting. For example using jQuery.Load

我认为最简单的方法是使用客户端脚本。例如,使用jQuery.Load

#1


11  

Disclaimer: The proposed solution below is a hack.

免责声明:以下建议的解决方案是黑客攻击。

Suppose that you have the following controller:

假设您有以下控制器:

public class HomeController : Controller
{
    [ChildActionOnly]
    public ActionResult Foo()
    {
        return View();
    }
}

and a corresponding partial (Foo.ascx).

和相应的部分(Foo.ascx)。

And now in a legacy WebForms page you wanted to use this action. Here's how you could proceed:

现在,在遗留的WebForms页面中,您想要使用此操作。以下是您可以继续的方式:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script type="text/C#" runat="server">
    private class DummyController : Controller { }

    public HtmlHelper Html { get; private set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        var httpContext = new HttpContextWrapper(Context);
        var controllerContext = new ControllerContext(
            httpContext, 
            new RouteData(), 
            new DummyController()
        );
        var viewContext = new ViewContext(
            controllerContext, 
            new WebFormView("Views"), 
            new ViewDataDictionary(), 
            new TempDataDictionary(), 
            TextWriter.Null
        );
        Html = new HtmlHelper(viewContext, new ViewPage());  
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%= Html.Action("About", "Home") %>
    </div>
    </form>
</body>
</html>

#2


2  

I used this code in my MasterPage and it worked. But after days I have found that my application was restarting every minute because of memory leaks. I was hunting days for the cause and finnally, this part of code was the cause of memory leak. You have to dispose DummyController and ViewPage. I have fix it like this:

我在我的MasterPage中使用了这段代码并且它有效。但几天后我发现我的应用程序因为内存泄漏而每分钟都重启。我正在寻找原因和最终,这部分代码是内存泄漏的原因。你必须处理DummyController和ViewPage。我这样解决了:

 public class BaseMaster :  System.Web.UI.MasterPage
 {

#region MVC
    Controller _controller= new DummyController();
    ViewPage _viewPage = new ViewPage();

    private class DummyController : Controller { }

    public HtmlHelper Html { get; private set; }

    private void InitHtmlHelper()
    {          
        var httpContext = new HttpContextWrapper(Context);
        var controllerContext = new ControllerContext(
            httpContext,
            new RouteData(),
            _controller
        );
        var viewContext = new ViewContext(
            controllerContext,
            new WebFormView(controllerContext,"Views"),
            new ViewDataDictionary(),
            new TempDataDictionary(),
            TextWriter.Null
        );
        Html = new HtmlHelper(viewContext, _viewPage);  
    }


    public override void Dispose()
    {
        base.Dispose();

        _controller.Dispose();
        _viewPage.Dispose();
    }

#endregion


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            InitHtmlHelper();
            ...
            ...
        }

        ...
   }

#3


0  

I think the easiest would be using client-side scripting. For example using jQuery.Load

我认为最简单的方法是使用客户端脚本。例如,使用jQuery.Load