1

I'm in a situation where I'm new to MVC and the other programmer on my team is not so sometimes my learning curve is slowing deliverables down a little, so I'm trying to figure out what my options are for mixing in MVC views (or partial views) with WebForms as a short term solution to make functionality available in beta release and give me a chance to re-write in MVC for production.

I read this article and it explains how to reference assemblies of both frameworks but it doesn't seem to explain how to mix them in a single page.

For instance, can I take a user control (.ascx) and a view (.cshtml) and easily render them both into a master page (.master.aspx)?

It's as if I'd like a server control that executed the view on the server side and writes the contents to the Response stream. I guess the other option is to javascript ajax the view into the asp.net page but I'm wondering if that can be avoided.

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • 1
    I don't have a definitive answer for you, as with the project that I'm working on we ran into some serious limitations with mixing WebForms and MVC (primarily that there are serious problems with viewstate). You might have luck by [rendering a view as a string](http://stackoverflow.com/questions/483091/render-a-view-as-a-string). – Mike Evans Aug 30 '12 at 23:00
  • hey, that's not bad... thanks – Aaron Anodide Aug 30 '12 at 23:04
  • You might like to look up [MVP Web Forms](http://webformsmvp.com/) developed by two ASP.Net MVPs. – Jeremy Thompson Aug 31 '12 at 00:56

1 Answers1

2

Yes and no.

You can mix ASP.NET MVC and WebForms, but it isn't recommended, and there are also restrictions on what you can mix. Throughout my post I'm referring to ASPX views, not Razor / CSHTML views. Using alternative ViewEngines with WebForms is almost impossible.

An MVC View (ASPX, not CSHTML) by itself can be used by WebForms provided that you initialise it correctly and provide a Model object if required (a View is then just like a regular aspx page except without a CodeBehind class).

You can also load ASCX controls and System.Web.UI.Control subclasses into an ASP.NET MVC view with a few minor hacks (it isn't perfect - control lifetime events won't be fired, for example, but it's more about getting them to render into your view).

You can also have ASPX pages in the same environment as an MVC web application (so long as you get Routing working). A CodeBehind-style ASPX can redirect users to an MVC Controller action and vice-versa.

However you cannot invoke MVC Controller actions from within a WebForms page CodeBehind without a lot of uphill work, but I can't really see any reason why you'd want to.

I think you should reword your original question and think about what you ultimately want to accomplish. Personally if this was a maintenance task I'd stick with pure WebForms and only use MVC for new projects. WebForms isn't dead, just ask ScottGu.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • thanks for the advice - you aswered my question I believe sufficiently, although I'm not entirely sure how to change the wording - feel free to edit it if you wish – Aaron Anodide Aug 30 '12 at 23:01