如何将一个WebForm里面的局部视图 [英] How to include a partial view inside a webform

查看:799
本文介绍了如何将一个WebForm里面的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些网站我的编程同时使用ASP.NET MVC和WebForms的。

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

我有一个局部视图,我想这包含一个表单中。局部视图有一些code具有服务器进行处理,因此使用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.

我怎样才能做到这一点?

How can I do this?

推荐答案

我看了一下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(而不是像HttpContext的做的WebForms),并同时都实现的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() ); %>

这篇关于如何将一个WebForm里面的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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