2011-04-10 59 views
2

我最近已將我的應用程序升級到.net 4.0。我的超鏈接導航相對路徑被正確渲染時遇到問題。我通過jquery調用webmethod,加載一個usercontrol並獲取我在頁面上顯示的html。 usercontrol具有一個帶有超鏈接字段的網格視圖,該字段使用波浪號(〜)綁定到某些應用程序相對路徑。與Ajax調用不正確的超鏈接相對路徑

對於複製此問題,我創建了一個網站在IIS中的「默認網站」下面說「TestWebsite」。我在根文件夾下有一個「Test.aspx」頁面。另外我有UserControls文件夾,它具有我的用戶控件「TestUserControl.ascx」。現在我只需在頁面中調用webmethod,它將加載控件並返回html。我遇到的問題是超鏈接URL相對路徑渲染不正確。它顯示的

http://localhost/SubFolder/Sample1.aspx,而不是與我的根文件夾(TestWebsite)

http://localhost/TestWebsite/SubFolder/Sample1.aspx開始。

下面是示例代碼

Test.aspx文件

<div> 
    <script type="text/javascript"> 

     $(document).ready(function() { 
      $.ajax({ 
       type: "POST", 
       url: "Test.aspx/TestMethod", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
        $("#result").html(msg.d); 
       } 
      }); 
     });   
</script> 

</div> 

<div id="result"></div> 

Test.aspx.cs - 的webmethod

[WebMethod] 
public static string TestMethod() 
{ 
    StringWriter tw = new StringWriter(); 
    Control control = null; 

    Page page = new Page(); 
    try 
    { 
     control = page.LoadControl("~/UserControls/TestUserControl.ascx"); 
    } 
    catch (Exception ex) 
    { 
     string name = ex.Message; 
    } 

    HtmlForm form = new HtmlForm(); 
    form.ID = "_form"; 
    page.Controls.Add(form); 

    form.Controls.Add(control); 

    page.Controls.Add(form); 

    HttpContext.Current.Server.Execute(page, tw, true); 

    string html = tw.ToString(); 
    return html; 
} 

用戶控件

protected void Page_Load(object sender, EventArgs e) 
{ 
    DataTable dt = new DataTable(); 
    dt.Columns.Add("TestLink"); 
    DataRow dr = dt.NewRow(); 
    dr["TestLink"] = "~/SubFolder/Sample1.aspx"; 
    dt.Rows.Add(dr); 

    dr = dt.NewRow(); 
    dr["TestLink"] = "~/SubFolder/Sample2.aspx"; 
    dt.Rows.Add(dr); 

    GVTest.DataSource = dt; 
    GVTest.DataBind();    
} 

TestUserControl.ascx

Iam由於格式問題而無法在此處複製我的代碼,但我有一個簡單的GridView,這裏有一個超鏈接服務器控件,用於綁定navigateurl。

網絡上的一些搜索後,我發現,在其中提及設置AppRelativeTemplateSourceDirectory

Asp.Net single control render for AJAX calls

這種類似的問題。但即使將其設置爲AppDomainAppVirtualPath之後,它也不適用於我。

使用Server.Execute後相對URL(〜/子文件夾/)變成了給(../SubFolder/)一級

我不知道如果我缺少什麼?如果有人能幫我解決這個問題,我將不勝感激。

FYI:我使用IIS 7.5和Windows 7

感謝, PRASHANT

回答

1

我有在以同樣的方式呈現的控制,我認爲你正在經歷相對URL相同的問題。

我的問題是,我注意到request.path是相對於服務調用,顯然AJAX或IIS在您當前頁面下創建一個子目錄,這就是爲什麼ResolveClientUrl或「代字號」解析爲一個額外的父目錄...

所以你的情況我的猜測是,你的控制是在

http://Test.aspx/TestMethod(將WebMethod名)

這實際上是正在發生的事情對我來說實際創建,我的所有URL有一個額外的。 \ prepended。 Marty

0

Marty描述的一切都是正確的,我不確定如何解決這個問題,但是這裏有一個我使用的解決方法,它檢測何時多餘的「../」被預置並糾正了情況通過將子目錄讀入網址:

 // this is the link that will be appended to the fullpath+subdirectory of the app 
     var resolvedLinkInBrowser = this.ResolveClientUrl(builtUrl); 

     if (resolvedLinkInBrowser.Contains("../")) 
     { 
      // when an ajax call is made, it is made using a service call. This service call for some 
      // reason does not think that it should serve links relative to the subdirectory, but thinks 
      // that it should serve links relative to the root of the domain. That is why when the link is resolved, 
      // WebForms adds a '../' to the link, to get it back to the root domain. Since we've detected this case, 
      // we can insert the subdirectory in to our not-yet-resolved url, to force the resulting url to be correct. 
      // this is a workaround. If this issue can be fixed any other way, please do that. 
      builtUrl = builtUrl.Replace("~/", "~" + HttpRuntime.AppDomainAppVirtualPath + "/"); 
     }