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?
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.
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).
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.
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With