ResolveUrl without an ASP.NET Page

折月煮酒 提交于 2019-11-27 19:34:25
BrunoLM

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
davidsleeps

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!

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!