如何在网络表单中包含部分视图 [英] How to include a partial view inside a webform

查看:26
本文介绍了如何在网络表单中包含部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写的某些站点同时使用 ASP.NET MVC 和 WebForms.

Some site I'm programming is using both ASP.NET MVC and WebForms.

我有一个局部视图,我想将其包含在网络表单中.局部视图有一些必须在服务器中处理的代码,因此使用 Response.WriteFile 不起作用.它应该在禁用 javascript 的情况下工作.

I have a partial view and I want to include this inside a webform. The partial view has some code that has to be processed in the server, so using Response.WriteFile don't work. It should work with javascript disabled.

我该怎么做?

推荐答案

我查看了 MVC 源代码,看看我是否能弄清楚如何执行此操作.控制器上下文、视图、视图数据、路由数据和 html 渲染方法之间似乎有非常紧密的耦合.

I had a look at the MVC source to see if I could figure out how to do this. There seems to be very close coupling between controller context, views, view data, routing data and the html render methods.

基本上,为了实现这一点,您需要创建所有这些额外的元素.其中一些相对简单(例如视图数据),但有些更复杂 - 例如路由数据会认为当前的 WebForms 页面被忽略.

Basically in order to make this happen you need to create all of these extra elements. Some of them are relatively simple (such as the view data) but some are a bit more complex - for instance the routing data will consider the current WebForms page to be ignored.

最大的问题似乎是 HttpContext - MVC 页面依赖于 HttpContextBase(而不是像 WebForms 那样的 HttpContext),虽然两者都实现了 IServiceProvider,但它们并不相关.MVC 的设计者深思熟虑地决定不更改旧的 WebForms 以使用新的上下文库,但他们确实提供了一个包装器.

The big problem appears to be the HttpContext - MVC pages rely on a HttpContextBase (rather than HttpContext like WebForms do) and while both implement IServiceProvider they're not related. The designers of MVC made a deliberate decision not to change the legacy WebForms to use the new context base, however they did provide a wrapper.

这行得通,让您可以向 WebForm 添加部分视图:

This works and lets you add a partial view to a WebForm:

public class WebFormController : Controller { }

public static class WebFormMVCUtil
{

    public static void RenderPartial( string partialName, object model )
    {
        //get a wrapper for the legacy WebForm context
        var httpCtx = new HttpContextWrapper( System.Web.HttpContext.Current );

        //create a mock route that points to the empty controller
        var rt = new RouteData();
        rt.Values.Add( "controller", "WebFormController" );

        //create a controller context for the route and http context
        var ctx = new ControllerContext( 
            new RequestContext( httpCtx, rt ), new WebFormController() );

        //find the partial view using the viewengine
        var view = ViewEngines.Engines.FindPartialView( ctx, partialName ).View;

        //create a view context and assign the model
        var vctx = new ViewContext( ctx, view, 
            new ViewDataDictionary { Model = model }, 
            new TempDataDictionary() );

        //render the partial view
        view.Render( vctx, System.Web.HttpContext.Current.Response.Output );
    }

}

然后在您的 WebForm 中,您可以执行以下操作:

Then in your WebForm you can do this:

<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>

这篇关于如何在网络表单中包含部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆