use web form master page as master page for ASP.Net MVC views programatically
Asked Answered
C

3

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.

Cradlesong answered 19/12, 2011 at 16:34 Comment(0)
S
3

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.

Sallee answered 27/12, 2011 at 17:41 Comment(5)
thanks for great posts but it talk about master page inside web project not as class library project(dll).Cradlesong
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?Hexad
@HaBo: I don't understand your question exactly. The posts talk about using an Webforms Masterpage with ASP.NET MVC Razor Views.Sallee
Sorry for Confusing. I am clear now. Thanks for the informationHexad
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.Ilyse
R
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.

Regnal answered 28/12, 2011 at 20:14 Comment(0)
R
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);
Rania answered 31/12, 2011 at 16:52 Comment(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).Cradlesong

© 2022 - 2024 — McMap. All rights reserved.