5

I'm currently having trouble finding a way to call ResolveClientUrl within the context of a static web method inside of a ASP.Net Web Forms page.

I'm using jQuery Ajax calls to interact with WebForms as documented here: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ which is the reason why the WebMethod needs to be static. Problem is that within the WebMethod I need to generate an URL and append a query string to it, and I would like to play it safe and pass it through ResolveClientUrl before appending the query string.

Is there any way I can work around this, or does .Net provide an alternate static method that does more or less the same thing?

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
enriquein
  • 1,048
  • 1
  • 12
  • 28

2 Answers2

11

from... ASP.Net C# ResolveClientUrl inside Class

Instead of calling ResolveClientUrl on the Page object (or any controls), you can also use Web.VirtualPathUtility.ToAbsolute("~/home.aspx"); which will give you the same result as calling ResolveUrl("~/home.aspx");

Community
  • 1
  • 1
Carter Medlin
  • 11,857
  • 5
  • 62
  • 68
2

It is possible if called from a web page using:

public static void DoThis() { Page page = HttpContext.Current.Handler as Page; }

However, if you are in a web method, it's not going to be the page as the handler; its a handler for the web request. I used this approach from JavaScript, and it did work:

http://iridescence.no/post/Resolving-relative-URLe28099s-from-JavaScript.aspx

HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • It almost feels like a *hack*. However, it's dead simple and it gets the job done. Also, I feel like it's my only choice. Great job, thanks! – enriquein Jul 15 '11 at 17:46