Change Master Page depending on URL

Niall Gallagher
Niall Gallagher used Ask the Experts™
on
Change MasterPage depending on URL. Is  this possible.

I have 2 departments at work that both use the same site I built but they want to call it by different names. So when I built it back in 2014 I published it twice with two different titles and different name in the master page. So what I want to do (if possible) is put 2 urls to the same site but depending what url they use will affect the masterpage, eg wether the title will say Dept A or Dept B and change the label at the top of the master page to say Dept A or Dept B, or would it be any easier to just make 2 different Master pages and somehow load a different one depending on url.
I have no idea how to go about this so any ideas are greatly appreciated :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Niall,

You can use Page.PreInit https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.preinit?view=netframework-4.8

And based on the conditions you need to change Page.MasterPageFile https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.masterpagefile?view=netframework-4.8 in PreInit method mentioned above.

Regards,
Chinmay.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
How you'll approach this will depend on if both site's somehow share the same data or there are 2x different data sets, one specific to each site.

Tip: If something's working, think twice about changing it up.
Niall GallagherDeveloper

Author

Commented:
David, They are the exact same site, hitting the exact same DB, and returning the exact same Data, just both offices want to call it different names
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Niall,

In that case, just check the URL in Page_Load and change Page.Title and The Labels. I would not recommend going after a separate Master page unless you see a need in future where there can be more than couple of controls to change.

Regards,
Chinmay.
Niall GallagherDeveloper

Author

Commented:
I will try this tomorrow, What would be the best way to check the url they used
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Use Request.Url property and its sub properties per your requirements.
https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.url?view=netframework-4.8
Niall GallagherDeveloper

Author

Commented:
Thank you,
This has definitely helped me to tackle this problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial