开发者

Include MVC 3 views in a Web Forms application

I am developing one new area of a fairly large web application u开发者_如何学编程sing MVC3 and Razor, where the rest of the application is ASP.NET 4 Web Forms based. Can I include my MVC components in this application, and what do I have to do to achieve this?


Our experience has been that WebForms and MVC work well side-by-side in the same application, but not mixed. In other words, each page can either be WebForms or MVC, but it can't very easily include elements of both. The two main work-arounds I've found are:

  • Make all your MVC elements heavily AJAXified, so that they can be loaded dynamically via AJAX after the page loads.
  • Have two versions of your master pages, and anything else "common" to your web application, to make the MVC portions of your application have the same look and feel as the WebForms portions.

We use the latter approach.


http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc

That should get you on the right road. If it was me personally though, i'd add a new project to your solution that is MVC, you can then just configure a Virtual Directory in IIS /MVCApp


I am using a class like this one to embed an MVC view into webforms page. Will try to do that in a reverse direction

public class HelperFactory
{
    private class FakeController : Controller
    {
    }

    private class FakeView : IView
    {
        public void Render( ViewContext viewContext, TextWriter writer )
        {
            throw new NotImplementedException();
        }
    }

    public static HtmlHelper<TModel> GetHelper<TModel>()
    {
        //HttpContextBase context = new HttpContext( HttpContext.Current );
        FakeController controllerBase = new FakeController();
        RouteData rd = new RouteData();
        rd.Values.Add( "controller", "Fake" );
        RequestContext requestContext = new RequestContext( new HttpContextWrapper( HttpContext.Current ), rd );
        ControllerContext fakeContext = new ControllerContext( requestContext, controllerBase );
        ViewDataDictionary vdd = new ViewDataDictionary();
        ViewContext viewCtx = new ViewContext( fakeContext, new FakeView(), vdd, new TempDataDictionary(), requestContext.HttpContext.Response.Output );

        return new HtmlHelper<TModel>( viewCtx, new ViewPage() );
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜