开发者

Problems displaying aspx pages in different folder than master page

开发者 https://www.devze.com 2023-03-13 02:53 出处:网络
I have a site master page which uses some JQuery scripts, when my site accesses anything that is in my Account folder, the page loads but not without an error message as well as a messed up master pag

I have a site master page which uses some JQuery scripts, when my site accesses anything that is in my Account folder, the page loads but not without an error message as well as a messed up master page. 开发者_如何学运维It works fine if I pull the page out from the Account folder. Trying the entire day to figure this out but to no avail. Appreciate any help given. Thanks!


It'll be to do with your links to CSS and JavaScript. Ensure your add "../" to go up a level in folder structure.


I think there may be security issue

First ensure that you added proper realtive path of the javascript file in you file...

and

Read this : Setting authorization rules for a particular page or folder in web.config

allow an anonymous user to gain access the Account.aspx page. Copy

<configuration>
   <location path="~/Users>
      <system.web>
         <authorization>
            <allow users="?"/>
         </authorization>
      </system.web>
   </location>
</configuration>


We don't really have sufficient information to provide a definitive answer here - for instance, what do you mean by 'an error message'? What is the source? How is it displayed? and, most importantly, what does it say? What happens if you remove use of jQuery, does the page display and function as expected?

That aside, to account for a 'messed up MasterPage', I would assume that your Account folder has restricted access via authorization section of the web.config. This can be adjusted to allow access to certain resources; in the authorization section, you may have something like this:

<authorization>
    <deny users="?"/>
    <allow users="*"/>
</authorization>

The above will lock out access to all files that fall within the path that this is within scope of. Naturally your login page will be accessible, but maybe not resources such as CSS files and images - to allow these to be accessed by users not yet authenticated you can configure a custom location, as such:

<location path="pathToResources">
    <system.web>
        <authorization>
            <allow users="?"/>
        </authorization>
    </system.web>
</location>


Well, ASP.NET interprets relative links from the "viewpoint" of the page, so if the page is in folder, your master page relative links will not work anymore.

Here is the solution that I use, wrap your head content in a custom control which will do the rebase:

<asp_custom:RebasingContainer ID="mainRebase" runat="server">
</asp_custom:RebasingContainer>  

for and use runat=server and use them like this:

<link rel="stylesheet" type="text/css" href="~/css/reset.css"/>

Note the "~" which is ASP.NET "go from root" path.

For controls, use this:

[ControlBuilder(typeof(RebasingContainerBuilder)),
  Designer("System.Web.UI.Design.ControlDesigner, System.Design, " +
  "Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
  ConstructorNeedsTag(false)]
public class RebasingContainer : HtmlGenericControl
{
    public RebasingContainer()
    {

    }

    protected override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
    {  /*doesn't render it's own tag*/ }

    protected override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
    {/*doesn't render it's own tag*/}
}

The control uses following control builder:

public class RebasingContainerBuilder : ControlBuilder
{
    public override bool AllowWhitespaceLiterals()
    {
        return false;
    }

    public override Type GetChildControlType(string tagName, System.Collections.IDictionary attribs)
    {
        if (string.Equals(tagName, "link", StringComparison.OrdinalIgnoreCase))
        {
            return typeof(HtmlLink);
        }

        if (string.Equals(tagName, "script", StringComparison.OrdinalIgnoreCase)
            && attribs.Contains("src"))
        {
            //only rebase script tags that have a src attribute!
            return typeof(HtmlScript);
        }

        return null;
    }
}

Where the script is:

public class HtmlScript : HtmlGenericControl
{
    public HtmlScript() : base("script") { }

    public HtmlScript(string tag) : base(tag) { }

    public string Src
    {
        get
        {
            return this.Attributes["src"];
        }
        set
        {
            this.Attributes["src"] = value;
        }
    }

    protected override void RenderAttributes(HtmlTextWriter writer)
    {
        Src = ResolveClientUrl(Src);
        base.RenderAttributes(writer);
    }
}

Register your customer rebase control in web.config and you are ready to go. For example:

<add assembly="__code" namespace="CustomControls" tagPrefix="asp_custom" />

if you use AppCode folder.

This solution will give you runtime as well as design time support for separated master and simple web pages.


simply use this:

putting / before js do the trick.

<script src="/js/jquery.js" type="text/javascript"></script>

Referenced from here : Using JQuery in a Subfolder When the MasterPage is in the Root Folder


When you have javascript or jQuery in a subfolder and pages in the root folder with some in a subfolder, do this: add two links for each javascript file in your masterpage:

eg:

<script src="Scripts/jquery.js" type="text/javascript"></script>
<script src="../Scripts/jquery.js" type="text/javascript"></script>

How it works: Pages in the subfolders will be referenced to the link with ../, i.e

<script src="../Scripts/jquery.js" type="text/javascript"></script>

Pages in root folder will be referenced to the link without ../, i.e

<script src="../Scripts/jquery.js" type="text/javascript"></script>

Tip: If you can't get one folder working or the other, play with the: ~/, /, ../, and ./ one will reference it for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号