ResolveUrl without an ASP.NET Page

后端 未结 3 1112
-上瘾入骨i
-上瘾入骨i 2020-12-05 03:36

I am looking for a way to resolve a relative url the way you would with a page or control instance (MSDN Docs) such as:

Page.ResolveUrl(\"~/common/Error.aspx         


        
相关标签:
3条回答
  • 2020-12-05 04:14

    Try to get the page from the handler and use ResolveUrl, or create a Control object...

    (HttpContext.Current.Handler as Page).ResolveUrl("~/virtualpath");
    

    Or use VirtualPathUtility.ToAppRelative(string) or VirtualPathUtility.ToAbsolute(string)

    For example:

    System.Web.VirtualPathUtility.ToAbsolute("~/Styles/Contoso.xslt");
    

    returns

    /WebSite/Styles/Contoso.xslt
    
    0 讨论(0)
  • 2020-12-05 04:15

    Use something like this - Controls is a folder name in your application and myController is the controller name. to create and instance and load the controller you can do it by:

    Controls_myController ctrl = Page.LoadControl(Page.ResolveUrl("controls/myController.ascx"));
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-05 04:26

    This question on SO (ASP.Net: Using System.Web.UI.Control.ResolveUrl() in a shared/static function) looks kind of helpful...Basically, you can use the VirtualPathUtility class which is under the System.Web namespace. There is an additional answer to that question which says to be careful of QueryString parameters, but a solution to that is also provided.

    At the same time, Rick Strahl's code is pretty neat!

    0 讨论(0)
提交回复
热议问题