Some site I’m programming is using both ASP.NET MVC and WebForms.
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?
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.
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.
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.
This works and lets you add a partial view to a WebForm:
Then in your WebForm you can do this: