Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling ResolveClientUrl within a static web method (ASPNet Web Forms)

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?

like image 372
enriquein Avatar asked Jul 15 '11 15:07

enriquein


People also ask

Do WebMethods have to be static?

As we know, the property of a static method “We can invoke a static method using it's class name without creating an object of this specific class”. This is the reason it's necessary to declare WebMethod as a static method.

Why Web method is static?

They're static because they are entirely stateless, they don't create an instance of your page's class and nothing is passed to them in the request (i.e. ViewState and form field values).

How do I call a normal method on WebMethod?

As the WebMethod is static, it cannot call one normal method of the Page class. If you want to call one method inside the WebMethod, you need to mark it as Static.


2 Answers

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");

like image 93
Carter Medlin Avatar answered Oct 04 '22 03:10

Carter Medlin


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.

like image 20
Brian Mains Avatar answered Oct 04 '22 05:10

Brian Mains