I have working ASP.NET MVC 4 project. I want to add to this MVC project 2 .aspx pages of another WebForms project. I have several questions:
- Where should I copy this
.aspxfiles and how should I config my
routes? - How should I tell this
.aspxpages to use~/Shared/_Layout.chtmlas a master page? - Additionally this .aspx pages uses .ascx controls. Where should I store them?
- How should I modify web.config file?
I have looked at the links posted in this question, but they seemed to be outdated. A lot of the links explain how to add MVC to WebForms, but I am looking for all the way around.
Any useful links would be appreciated. Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The solution:
As it turned out, adding .aspx pages to the existing MVC project is an even easier task to accomplish than adding mvc to .aspx. The most interesting thing for me was to find out that both webforms and MVC, in the scope of one project share one runtime on IIS.
So what I did:
- Added .aspx pages from another project to the root of my MVC project
- Created new master page for my webforms pages(right click project->add->new item->Master Page)
- In order to make Master Page of WF and _Layout.chtml of MVC share the same Master ‘View’ I found this great article that allows you to call something similar to @Html.RenderPartial() method within .aspx page
-
The following code provides information how to implement RenderPartial method in WebForms:
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 ); } }Add it to codebehind.cs of your .aspx page. Then you can call it from the webforms like this:
<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
-
Since I had only the ‘menu’ shared among all my pages, I added it to the partial View, then called it in _Layout.chtml
@Html.Partial("_Menu")
and in MasterPage.Master like this:
<% WebFormMVCUtil.RenderPartial("_Menu", null ); %>
That is all there is to it. As a result my _Layout.chtml and MasterPage.Master use the same shared partial views. And I can acess .aspx pages simply by navigating on them. If you have some issues with routing system, you can add routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); to your routeConfig in App_Start.
Sources I used:
- Combine asp net mvc with webforms
- Mixing Web Forms and ASP.NET MVC
- MixingRazorViewsAndWebFormsMasterPages
- How to include a partial view inside a webform
I hope it will help somebody later on.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0