开发者

incorrect hyperlink relative paths with ajax calls

I have recently upgraded 开发者_如何学Pythonmy application to .net 4.0. I have a problem with the hyperlink navigateurl relative paths being rendered properly. I am calling a webmethod through jquery which loads a usercontrol and get the html which I display it on the page. The usercontrol has a gridview with a hyperlink field which is bound to some application relative paths using tilde(~).

For replicating this issue I have created a website say "TestWebsite" under "Default Website" in IIS. I have a page "Test.aspx" under the root folder. Also I have UserControls folder which has my usercontrol "TestUserControl.ascx". Now I just call my webmethod in the page which will load the control and return the html. The issue I am having is the Hyperlink url relative path is rendered incorrectly. It displays

http://localhost/SubFolder/Sample1.aspx instead of starting with my root folder (TestWebsite)

http://localhost/TestWebsite/SubFolder/Sample1.aspx .

Here is the sample code

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;
}

UserControl

 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 not able to copy my code here due to formatting issues but I have a simple gridview here with a hyperlink server control where I bind the navigateurl.

After some search on the web I found this similar issue at

Asp.Net single control render for AJAX calls

where it was mentioned to set AppRelativeTemplateSourceDirectory. but even after setting this to AppDomainAppVirtualPath, it doesn't work for me.

After Server.Execute the relative url (~/SubFolder/) changes to one level up giving (../SubFolder/)

I am not sure if I am missing anything? I would appreciate if any one could help me on resolving this issue.

FYI: I am using IIS 7.5 and windows 7.

Thanks, Prashant


I have the same issue with relative URLs in a rendered control in the same fashion I think that you are experiencing.

My problem is that I noticed the request.path is relative to the service call, apparently AJAX or IIS creates a subdirectory under your current page and that's why ResolveClientUrl or "tilde" resolves to an extra parent directory ...

So in your case my guess is that your control is actually created in

http://Test.aspx/TestMethod (the name of the WebMethod)

This is actually what is happening to me, all my Urls have an extra ..\ prepended. Marty


Everything that Marty described is correct, and I'm not sure how to fix this issue either, but here's a workaround that I use, which detects when the extra "../" is prepended and corrects the situation by readding the subdirectory into the URL:

        // 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 + "/");
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜