Is it possible to share a masterpage between MVC and webforms? - asp.net-mvc

I am adding MVC to a project that has MANY legacy webform pages. This works fine. However, I currently have a separate masterpage for MVC and for the webforms. The two master pages produce essentially identical output. I'd really like to kill the webforms one and just use the MVC master page with all my pages and stay DRY.
Not being DRY has already bitten me a couple times when I forgot to change both.
I tried doing the obvious way and just pointing the webform content page's MasterPage attribute at the MVC masterpage. This throws an error saying the MVC masters only work with MVC views.
This seems like it would be a pretty common problem with mixed MVC and webform projects. My MVC master isn't doing anything with ViewData, so I don't see any reason the webforms couldn't use them.

You can absolutely share the same master page. Your MVC master page must simply point to the WebForms masterpage via its MasterPageFile attribute. This applies your WebForms MasterPage styles to your MVC MasterPage.
I am using this setup in production.
The declaration on my MVC Master Page, pointing at the Web Forms Master Page:
<%# Master Language="C#" MasterPageFile="~/MasterPage/Site.Master"
AutoEventWireup="true" Inherits="System.Web.Mvc.ViewMasterPage" %>
Works like a charm.

This blog post walks you through the necessary steps to share WebForm and MVC master pages with little or no duplication. It also includes a sample project you can download, and I found it quite helpful.
One hiccup I ran into was that I was using a LoginStatus control in my header. LoginStatus must be inside a form so I couldn't use it in my root master page (not wanting to end up with nested forms on all my MVC pages). But that was a pretty easy control to replace with a simple code block in my root master page.

In my webforms app, my master page inherits from "HLPUSD.SMART.SMARTMaster" which is just the namespace for our application and then the name of the webform class.
In my MVC project, the master page inherits from "System.Web.Mvc.ViewMasterPage"
Me thinks this has something to do with it?

Related

Can MVC framework be used in a web application that is currently using ASP .NET?

I recently joined a group that manages a Classic ASP web application. It has been working fine for our group's need. However, a decision was made, before I joined, to move to ASP .Net. Since we are mostly ASP developers, we write code in ASP .Net as we would in Classic ASP (for the most part). Would it be possible to introduce MVC to this application/project?
Thanks!
Yes, it is possible to use MVC in a traditional WebForms project. I migrated a large WebForms project to MVC 2 a couple of years ago, and here are my findings (I have updated them to reflect MVC 3)
Make sure you have .NET 4.0 installed, as well as the MVC 3 framework and VS extensions.
Create a new blank MVC project to use as a reference.
Look at the default web.config for the reference project. You basically want to use the reference web.config, and merge in stuff you need from your current project.
Look at the reference global.asax.cs. Similar to the above, you want to merge the changes in the reference .cs into your current application's global.asax.cs.
You will need to add the following references to your web project:
System.Web.Abstractions,
System.Web.Extensions,
System.Web.Helpers,
System.Web.Mvc,
System.Web.Routing
You can enable the VS extensions by changing the ProjectTypeGuids:
In Solution Explorer, right-click the project name and select Unload Project. Then right-click the project name again and select Edit ProjectName.csproj.
Locate the ProjectTypeGuids element and add {E53F8FEA-EAE0-44A6-8774-FFD645390401}.
Save the changes, right-click the project, and then select Reload Project.
Add the following standard folders for MVC content:
~/Views
~/Views/Shared
~/Controllers
~/Models (for your view models, optional)
~/Content (for CSS and images, optional)
~/Scripts (for JS, optional)
Additional notes:
If your existing WebForms relies on web.config settings for authorization (such as preventing unauthorized users), this won't be recognized by MVC actions, because routing works completely separately from the WebForms authorization. Use AuthorizeAttribute to require authorization, or constrain actions to certain roles or users. You can even specify global filters so you don't have to apply this attribute on every single controller or action.
There may be additional considerations for making MVC work with IIS versions prior to 7, or with application pools that use the Classic pipeline. Consider using IIS 7+ with Integrated pipeline.
My notes above mainly involve getting the baseline of MVC working, which uses ASPX views. ASPX views use the same markup as the ASPX files you are used to in WebForms. You can also use the new Razor syntax (primer), which I highly recommend. You can use both ASPX and Razor view pages at the same time. However, you cannot use an ASPX master page on a Razor view (or vice versa). Also, MVC will find and use ASPX views before Razor views, so if you upgrade a view to Razor, delete the original ASPX. You will need to do a little additional work to enable Razor views. I'm trying to find my notes for enabling Razor. I'll update when I find them. Once you do have Razor installed and working, you can use this tool by Telerik to convert ASPX to Razor.
Here is a question on SO about a problem I'd had while upgrading. I'm only providing it because it covered some of the points I mentioned above in more detail. However, I was upgrading to MVC 2 at the time, so some of this stuff is out of date.
The short answer is yes it is possible.
Scott Hanselman has written about this topic before.
Depending on the skills in your team, you may well find it difficult to get up to speed - it is possible to write clean MVC style code in Classic ASP but most people don't.
It is of course technically possible. However, it sounds like it would be a cultural shock, as MVC works quite a bit differently than Classic ASP or ASP.NET WebForms. I think it would be worthwhile to do - or just stick with ASP.NET WebForms if that seems more natural. But if you've not yet fully committed to WebForms, MVC seems just as easy to move to, IMO.
Yes. I worked on a project that began as plain ASP.NET and later added some ASP.NET MVC pages. Eventually we liked MVC so much more that we eventually migrated all of our WebForms pages to use MVC instead. But the whole time, the two systems worked very well together.
Here's the only real gotcha that I can remember running into: WebForms works by having the entire page encased in a big <form> tag. Since HTML doesn't allow you to have nested <form> tags, you typically can't use MVC forms inside of a WebForms page. Either keep your WebForms pages separate from your MVC content, avoid using HTML forms in MVC content that may appear on a WebForms page, or use popup dialogs for your forms that get created outside of the WebForms DOM area.

Side-by-Side Asp.Net and MVC Razor

We have an existing ASP.Net Web Application. I would like to create an ASP.Net MVC Razor Application where the two applications will work together. A single Master Page would contain menu items that can call .aspx pages as well as Razor .cshtml pages.
I have seen an example using MvcContrib Portable areas utilizing Routing. This particular example has .aspx pages in both (the MVC was not Razor).
Is there an example out there that will show the two running side-by-side and the MVC is Razor? It would be best if I could download a visual Studio Solution so that I can run this.
I am not sure if the MvcContrib way is the latest and best way to achieve this.
I do not want to go Hybrid!
You don't need any other external librarry. You can always convert the existing ASP.NET web forms Project to be a Hybrid one which uses webforms and MVC. You need to add the required MVC assembly references and make some changes to the web.config and you are all set. Scott has a simple and awesome blog post about this where he explains how to do the conversion.
I scribbled a note about how to enable the MVC specific Context menu( Add Controller / Add View) in the hybrid project after conversion here

Upgrading ASP.NET MVC2 project to MVC3 and using mixed View Engines

Can I upgrade my MVC2 project to MVC3 and continue to use my ASPX views, AND at the same time start writing some new views in Razor?
More specifically, Can I take an ASPX view page, and a Razor control in it? Would such a thing be possible?
Any other things one should look out for?
You can definitely mix view engines across totally separate views. As for putting Razor "controls" in an ASPX page, if you use RenderPartial, it should work.
One thing I would watch out for is that if you use Master Pages then you may end up duplicating them for both view engines. I had a good ol' WebForms Master Page, used by all my regular .aspx content files, but creating a new view using Razor means I have to use a duplicate Razor layout page alongside that old Master Page.
I haven't looked into it too much, but at first try I get 'The file "~/Views/Shared/Site.Master" could not be rendered, because it does not exist or is not a valid page.' and certainly there's nothing in Scott Gu's blog entry introducing Razor to suggest you can combine them, but I could be all wrong here.
http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
For how to mix view engine master pages...

Scheduler with asp.net MVC

I want to use a Scheduler like Telerik Scheduler in my MVC project. The problem is that the Scheduler is a Asp.Net WebForm control. For this reason, I must create a WebForm page in my Mvc project to put the Scheduler control.
When I show the page, it work fine to render the layout of the control but if I try to interact with it; click for change date, change to day view to week view, the control don't change.
I know that postback doesn't work in mvc project but does it work in a WebForm page in a Mvc project? If it doesn't work, it is the reason why when I try to interact with the control, the control don't respond.
I think it's because the postback don't work and the Scheduler use 100 % Databinding where when I change date, the postback don't contain any data that I have changed and for this reason, the control can't change is layout.
Do you have any ideas about postback with WebForm in a MVC project?
What type of designs can I adopt? (Two differents projets: One for my Scheduler with WebForm and another for all the rest of my website in Mvc project)
Are there any other controls that are easy to use with Scheduler?
Does anyone have any tips and tricks when needing both a WebForms control and MVC control in an MVC project?
Hanselman has a good guide for mixing MVC and WebForms into the same project.
You can try DayPilot Pro (my product, commercial) which has both ASP.NET and MVC versions.
ASP.NET:
http://www.daypilot.org/
MVC
http://mvc.daypilot.org/

Using asp.net site.master and spark views

Is it possible to use my site.master master page ? I want to start using Spark more, but this project I am working on is using asp.net view pages as well.
I could of course convert the master page to an application.spark page, but I was wondering if it is at all possible to use the Site.Master I already have.
As far as i remember, had no problems with this. There are questions in stack that says there are.
Scenario that will work for sure - double your site.master in spark (make 2 master pages).

Resources