在 ASP.NET 表单页面中包括 MVC 部分视图

发布于 2024-09-18 15:27:26 字数 198 浏览 5 评论 0原文

我们在同一解决方案中拥有旧的 ASP.NET Forms 页面和新的 MVC 视图和部分视图。网站上的一些页面是 MVC,而遗留页面是 Forms。这些旧版表单页面之一是 .ascx 控件。

有什么方法可以将 MVC 部分视图 (.ascx) 插入到此 Forms .ascx 控件中吗?

We have old ASP.NET Forms pages and new MVC views and partial views in the same solution. Some pages on the site are MVC and legacy pages are Forms. One of these legacy Forms pages is an .ascx control.

Is there any way for me to insert an MVC partial view (.ascx) into this Forms .ascx control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开浅夏 2024-09-25 15:27:26

我使用这种技术将 MVC 部分嵌入到 Web 表单页面中。不确定它是否适用于网络表单用户控件,但应该是可能的。

步骤 1. 在应用程序的 MVC 部分中,创建以下辅助函数。这完成了所有艰苦的工作:

namespace MvcApplication
{
    // create a dummy controller
    public class DummyController : Controller
    {
    }

    public static class MvcPartialHelper
    {
        public static void RenderPartial(string partialViewName, object model)
        {
            ControllerContext controllerContext;
            HttpContextBase httpContextBase;
            IView view;
            RouteData routeData;
            ViewContext viewContext;

            httpContextBase = new HttpContextWrapper(HttpContext.Current);
            routeData = new RouteData();
            routeData.Values.Add("controller", typeof(DummyController).Name);
            controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController());
            view = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName).View;
            viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
            view.Render(viewContext, httpContextBase.Response.Output);
        }
    }
}

然后,在您的网页(或用户控件)中:

添加以下内容以引用上述内容:

<%@ Import Namespace="MvcApplication" %>

然后当您需要显示部分时,您可以添加类似的内容:

<% MvcPartialHelper.RenderPartial("~/views/shared/TestPartial.ascx", "hello - this is my model"); %>

其中第二个参数是您的“模型” '。

我在混合 MVC/Webforms 环境中广泛使用这种技术,它的效果就像梦一样!

享受

I use this technique to embed MVC partials into webforms pages. Not sure if it works in a webforms user control, but it should be possible.

Step 1. Within the MVC part of your application, create the following helper function. This does all the hard work:

namespace MvcApplication
{
    // create a dummy controller
    public class DummyController : Controller
    {
    }

    public static class MvcPartialHelper
    {
        public static void RenderPartial(string partialViewName, object model)
        {
            ControllerContext controllerContext;
            HttpContextBase httpContextBase;
            IView view;
            RouteData routeData;
            ViewContext viewContext;

            httpContextBase = new HttpContextWrapper(HttpContext.Current);
            routeData = new RouteData();
            routeData.Values.Add("controller", typeof(DummyController).Name);
            controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController());
            view = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName).View;
            viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
            view.Render(viewContext, httpContextBase.Response.Output);
        }
    }
}

then, within your web page (or user control):

add the following to reference the above:

<%@ Import Namespace="MvcApplication" %>

and then when you need to display the partial you can add something like:

<% MvcPartialHelper.RenderPartial("~/views/shared/TestPartial.ascx", "hello - this is my model"); %>

where the second parameter is your 'Model'.

I use this technique extensively in a mixed MVC/Webforms environment and it works like a dream!

Enjoy

强辩 2024-09-25 15:27:26

不,没有,因为您没有执行此插入所需的 Html 帮助程序:

<%= Html.RenderPartial("foo") %>

此外,您的 MVC 部分是强类型的(不是吗),您将无权访问模型。

此外,当您将旧版 Webforms 应用程序迁移到 ASP.NET MVC 时,情况也应该相反。

No, there isn't as you don't have the Html helper needed to perform this insertion:

<%= Html.RenderPartial("foo") %>

Also your MVC partial is strongly typed (isn't it) and you won't have access to the model.

Also when you are migrating a legacy webforms application to ASP.NET MVC it should be the other way round.

那请放手 2024-09-25 15:27:26

从技术上讲,这是可能的,尽管您需要跳过一些障碍才能实现您所要求的目标。您需要创建一个虚拟控制器上下文、视图上下文和相关环境,然后在代码后面的页面中创建一个属性来模拟模型。

如果您需要详细说明/示例,请告诉我

Technically it is possible, though you need to jump through some hoops to achieve what you are asking. You need to create a dummy controller context, view Context and related environment, and then create a property in your page behind code to simulate the model.

Let me know if you would like detailed instructions / example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文