[英]Problems displaying aspx pages in different folder than master page
我有一個站點母版頁,它使用一些 JQuery 腳本,當我的站點訪問我的帳戶文件夾中的任何內容時,頁面加載但並非沒有錯誤消息以及母版頁混亂。 如果我從帳戶文件夾中拉出頁面,它工作正常。 嘗試了一整天來解決這個問題,但無濟於事。 感謝提供的任何幫助。 謝謝!
這與您指向 CSS 和 JavaScript 的鏈接有關。 確保在文件夾結構中將“../”添加到 go 上一級。
只需使用這個:
把 / 放在 js 之前就可以了。
<script src="/js/jquery.js" type="text/javascript"></script>
我們真的沒有足夠的信息在這里提供明確的答案——例如,你所說的“錯誤信息”是什么意思? 來源是什么? 它是如何顯示的? 而且,最重要的是,它說了什么? 如果您刪除 jQuery 的使用會發生什么,頁面是否按預期顯示和 function?
除此之外,為了解決“混亂的 MasterPage”,我假設您的 Account 文件夾通過 web.config 的authorization
部分限制了訪問。 這可以調整以允許訪問某些資源; 在authorization
部分,你可能有這樣的東西:
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
以上將鎖定對 scope 路徑內的所有文件的訪問。 自然可以訪問您的登錄頁面,但可能無法訪問諸如 CSS 文件和圖像之類的資源 - 為了允許尚未經過身份驗證的用戶訪問這些資源,您可以配置自定義location
,如下所示:
<location path="pathToResources">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
我認為可能存在安全問題
首先確保您在文件中添加了 javascript 文件的正確實際路徑...
和
閱讀: 在 web.config 中為特定頁面或文件夾設置授權規則
允許匿名用戶訪問 Account.aspx 頁面。 復制
<configuration>
<location path="~/Users>
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
</configuration>
好吧,ASP.NET 從頁面的“視點”解釋相對鏈接,所以如果頁面在文件夾中,您的母版頁相對鏈接將不再起作用。
這是我使用的解決方案,將您的頭部內容包裝在一個自定義控件中,該控件將執行變基:
<asp_custom:RebasingContainer ID="mainRebase" runat="server">
</asp_custom:RebasingContainer>
for 並使用 runat=server 並像這樣使用它們:
<link rel="stylesheet" type="text/css" href="~/css/reset.css"/>
注意“~”,它是 ASP.NET “從根目錄”路徑。
對於控件,請使用:
[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*/}
}
該控件使用以下控件生成器:
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;
}
}
腳本在哪里:
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);
}
}
在 web.config 中注冊您的客戶變基控制,您就可以開始使用 go。 例如:
<add assembly="__code" namespace="CustomControls" tagPrefix="asp_custom" />
如果您使用 AppCode 文件夾。
此解決方案將為您提供運行時和設計時支持,以支持分離的主頁面和簡單的 web 頁面。
當您在子文件夾中有 javascript 或 jQuery 並且在根文件夾中有一些頁面在子文件夾中時,請執行以下操作:為您的母版頁中的每個 javascript 文件添加兩個鏈接:
例如:
<script src="Scripts/jquery.js" type="text/javascript"></script>
<script src="../Scripts/jquery.js" type="text/javascript"></script>
它是如何工作的:子文件夾中的頁面將被引用到帶有../
的鏈接,即
<script src="../Scripts/jquery.js" type="text/javascript"></script>
根文件夾中的頁面將被引用到不帶../
的鏈接,即
<script src="../Scripts/jquery.js" type="text/javascript"></script>
提示:如果您無法使一個文件夾或另一個文件夾正常工作,請使用: ~/
、 /
、 ../
和./
一個會為您引用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.