2011-04-10 60 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 + "/"); 
     }