9

We have a running solution contains many web forms application with the same master page, we created a class library project for master page and its controls and reference it as a dll inside each web form application and assign master page programatically

Now we will add two new application to our solution but we decided to use ASP.Net MVC 3 for building these new applications.

The problem is, we must use the same master page for the new applications, so how can we set the master page for our views using code?

Take care to remember our master page is a class library project.

3 Answers 3

3
+25

Look at this articles to get an overview about mixing Webforms with MVC:

Another interesting project is Zeus when it comes to a mixed Webforms/MVC solution. It contains helpers to use HtmlHelper and UrlHelper on Webform pages.

5
  • thanks for great posts but it talk about master page inside web project not as class library project(dll). Dec 28, 2011 at 15:43
  • This post talks about sharing a master page between ASPX engine and Razor Engine. Will this be the same approach to share a regular ASP.Net Mater page with ASP.Net MVC 3.0 Razor Engine project?
    – HaBo
    Dec 30, 2011 at 15:08
  • @HaBo: I don't understand your question exactly. The posts talk about using an Webforms Masterpage with ASP.NET MVC Razor Views.
    – Jan
    Dec 31, 2011 at 14:22
  • Sorry for Confusing. I am clear now. Thanks for the information
    – HaBo
    Jan 2, 2012 at 14:23
  • I tried "Using Razor Pages with WebForms Master Pages" and it works, but it is a .aspx page, My pages are in cshtml(MVC views). How can I use web form master page(Site.Master) in MVC views cshtml page. Aug 29, 2019 at 11:41
1

If you really want to go the MVC route moving forward then you should probably invest some time to convert your master page to a layout. Everything else would be a hack/workaround.

0
1

Amir, I uploaded a working sample at here. Steps,

1) Downloaded and converted a sample application from here.

2) Add a MVC 3 application in this solution and Add reference the class library of above solution.

3) Specify the MasterPage in MVC view(either in view, MasterPageFile="~/MasterPageDir/MasterPage.master" or in controller return View("Index",masterName: MasterPageVirtualPathProvider.MasterPageFileLocation);).

4) Put these lines in global.asax.

MasterPageVirtualPathProvider vpp = new MasterPageVirtualPathProvider();
HostingEnvironment.RegisterVirtualPathProvider(vpp);
1
  • +1 for you answer but it depends on VirtualPathProvider. unfortunately our old project doesn't use it :(. It seems I have to copy master page Html into my _layout view and make the styles and images refer to production version(online). Jan 2, 2012 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.