• Question

  • User1862224703 posted

     i am having problems using masterpages in subfolders. anything in the general folder i easily link to the masterpage and it works, but when i put it in to website/folder/file.aspx it won't link properly. PLEASE HELP ME!!!!!!!!!!!!!!!!

    Tuesday, February 12, 2008 5:02 AM

All replies

  • User1808462404 posted

    How are you referencing your master page? Is it referenced through the web.config? Is it referenced through the .aspx page? Check that you are NOT using relative paths like "../../MasterPage", instead use the web application root operator "~/MasterPage".

    A link that may be of interest: ASP.NET Web Site Paths

    <hints id="hah_hints"></hints>
    Tuesday, February 12, 2008 8:39 AM
  • User1862224703 posted

     hi its me again, i am referencing ~/master.master through file.aspx but it still don't work

    Wednesday, February 13, 2008 7:48 PM
  • User636579196 posted
    I think I may have a similar issue to the original poster. 

    In IIS 5.1 I have setup a virtual directory and made one of the subfolders an application:



    In the subfolder, I am using nested master pages; the parent does not need master pages.

    I find I need to have duplicate copies of the master pages in the parent folder for the site to compile in Visual Studio 2008.

    If I delete the Master pages off the parent, I get a compile error in the Default.aspx page in the subfolder referencing a master page:

    Error 4 The file '/wh4l/MasterPages/SpecificLayout.master' does not exist. C:\...\WebSites\wh4l\SomeApp\Default.aspx 1 

    The compiler is referencing master pages off the parent rather than the subfolder.


    If I delete the master pages from the subfolder, the solution compiles but I get a runtime error when I access the Default.aspx page on the subfolder that is referencing the master files:

    Parser Error Message: The file '/wh4l/SomeApp/MasterPages/SpecificLayout.master' does not exist.


    The master file is being referenced from the Default.aspx on the subfolder as a relative reference:

    <%@ Page Language="C#" MasterPageFile="~/MasterPages/SpecificLayout.master" ...


    The compiler prevents me from referencing the master pages on the parent folder:

    <%@ Master Language="C#" MasterPageFile="/MasterPages/SpecificLayout.master"

    Error 7 The virtual path '/MasterPages/SpecificLayout.master' maps to another application, which is not allowed. C:\...\WebSites\wh4l\SomeApp\Default.aspx 1 

    It seems that IIS is resolving master page references differently than the compiler; compiler resolution of master page references appears to be incorrect.

    Is there a way to change the reference or the configuration so the compiler uses the correct master pages?



    Wednesday, February 13, 2008 8:21 PM
  • User-1397819706 posted


    How the files and the folders are placed in your Visual Studio 2008 and IIS? I mean do you place the masterpage in the root folder of the application(VS2008)? Is the "/MasterPages/" is the website name or a subfolder's name? In the IIS, is the masterpage placed under the vitualPage folder or in the root of your App? If you can illustrate it using a fig. it will be very grate. Thank you. 

    Thursday, February 14, 2008 5:20 AM
  • User636579196 posted

    Both the root folder of the application (wh4l) and a subfolder (SomeApp) are designated as applications in IIS:

    C:\...\Visual Studio 2008\WebSites\wh4l\SomeApp

     I have identical sets of master pages in 2 locations; regular subdirectories off of the designated application directories:

    C:\...\Visual Studio 2008\WebSites\wh4l\MasterPages

    C:\...\Visual Studio 2008\WebSites\wh4l\SomeApp\MasterPages


    Thursday, February 14, 2008 11:55 AM
  • User40290331 posted


    I am not sure that I understand your problem.  A common problem is when a masterpage is in the root folder and you try to use it from an aspx page in a subfolder.  In such a case, all the links on the masterpage will fail unless they are run on the server.  So they have to be converted from HTML code to server controls.  Here is how that is done.

    Simply add the runat="server" property to the <head> tag


    <head runat="server">

    <link href="styles/MyStyleSheet.css" rel="stylesheet" type="text/css" />


    Use the <asp:Image tag.


    <asp:image Id="Image13"  runat="server" ImageUrl="~/images/walkingholidays.jpg" alternateText="Walking Holidays" width="192" height="77" borderwidth="0" />

    NOTE: Look very closly at ALL the wording.  You need the ID=, runat="server", ImageUrl =, the tilde (~) before the path and the alt property must be spelled out (AlternateText=) and border is now "borderwidth."  Any departure from this wording will screw you up. To find out more about this tag, go to the help menu in VWD and search for <asp:image in the index.

    Add an Id= property and the runat="server" property. Note that there is no tilde (~) before the path as there is in the above image example.


    <a Id="A1"  href = "Pages/Calendar.aspx" runat="server">CALENDAR</a>

    All of these examples assume that the MasterPage is in the root directory.

    Jim Webber, Sacramento

    Saturday, February 16, 2008 12:15 AM
  • User-553246482 posted


     thanks for that, I was looking on how to do that myself. however I still have trouble with linking external .js files in the head. how can I go about doing that?

    currently, I have a URL variable in the web.config and I'm using  

    src='<asp:Literal runat="server" text="<%$ AppSettings: URL %>"/>filename.js'
     to make the reference work even in sub folders. However, this can be troublesome in case someone cannot resolve the domain name of the server and is using the IP address or host name. is there a better way to do it?


    Monday, February 18, 2008 11:35 AM
  • User40290331 posted

     Beyondard and all,

    Recommend Scott Mitchel's excellent article at   http://aspnet.4guysfromrolla.com/articles/052505-1.aspx 

    It appears that the Page.ResolveUrl() method he describes at the bottom of his article may be a one size fits all solution to all these problems.  I have not tried it yet.

    So the solution to your problem, Beyondard, may be  

    text = <%=Page.ResolveUrl("~/scripts/filename.js") %> 

    or something like it. 

    Monday, February 18, 2008 12:59 PM
  • User-108210343 posted

    You can try using ./MasterPages/MasterPage1.master    ( single dot )

    instead of these ../MasterPages/MasterPage1.master



    Wednesday, May 7, 2008 7:53 PM
  • User-1309519041 posted

     u can set u r master page on preint  event dyanamicaly.

    Tuesday, May 27, 2008 7:48 AM
  • User-424642344 posted

    I do set it there. But I also have one set in the page attribute of the aspx page (so I could use design time editor) and I need to ensure that that version is ALSO available since it seems the .net internals are validating that that master page exists before the preinit event is called.

    Tuesday, May 27, 2008 3:03 PM
  • User-464547319 posted

     I had a somewhat same problem and found a solution by using this site.


    Thursday, April 9, 2009 7:10 PM
  • User19108150 posted

    An old topic I know, but I've just had a similar problem which was caused the directory settings.

    I found the solution in this article: ASP.NET: cant access master page from sub directory

    Saturday, September 26, 2009 9:48 AM
  • User-351672084 posted

     Beyondard and all,

    Recommend Scott Mitchel's excellent article at   http://aspnet.4guysfromrolla.com/articles/052505-1.aspx 

    It appears that the Page.ResolveUrl() method he describes at the bottom of his article may be a one size fits all solution to all these problems.  I have not tried it yet.

    So the solution to your problem, Beyondard, may be  

    text = <%=Page.ResolveUrl("~/scripts/filename.js") %> 

    or something like it. 

    I can't get this to work in an <asp:HyperLink control>. Here is what I'm using:

    <asp:HyperLink id="lnkSendEmail" runat="server" ImageUrl='<%= ResolveUrl("images/email.png") %>' NavigateUrl='<%# SendEmail(DataBinder.Eval(Container.DataItem, "resourceid").ToString()) %>'>Send an email message</asp:HyperLink></td><td>&nbsp;</td><td>

    The image does not display, any idea why?

    Tuesday, May 18, 2010 7:57 PM
  • User-2078221973 posted

    Try this   href='<%= ResolveUrl("~/style.css")%>'

    Saturday, July 27, 2019 8:53 PM