没有 ASP.NET 页面的 ResolveUrl
我正在寻找一种方法来解析相对 url,就像使用页面或控件实例一样 (MSDN Docs) 例如:
Page.ResolveUrl("~/common/Error.aspx");
...但是当我只有一个 HttpContext 可用时,例如当我在 HttpHandler 中时。
我是否需要使用自定义函数,例如此处看到的函数?
或者有没有办法获取页面使用的底层函数。
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");
...but when I only have an HttpContext available to me, such as when I am in a HttpHandler.
Will I need to use a custom function, such as the one seen here?
Or is there a way to get at the underlying function used by the Page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试从处理程序获取页面并使用
ResolveUrl
,或创建一个 Control 对象...
或使用
VirtualPathUtility.ToAppRelative(字符串)
或VirtualPathUtility.ToAbsolute(string)
例如:
返回
Try to get the page from the handler and use
ResolveUrl
, or create a Control object...Or use
VirtualPathUtility.ToAppRelative(string)
orVirtualPathUtility.ToAbsolute(string)
For example:
returns
这个问题关于SO(ASP .Net:在共享/静态函数中使用 System.Web.UI.Control.ResolveUrl())看起来很有帮助......基本上,您可以使用 VirtualPathUtility 类位于 System.Web 命名空间下。该问题还有一个额外的答案,其中提到要小心 QueryString 参数,但也提供了解决方案。
同时,Rick Strahl 的代码非常简洁!
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!
使用这样的东西 -
Controls 是应用程序中的文件夹名称,myController 是控制器名称。要创建、实例和加载控制器,您可以通过以下方式完成:
希望这会有所帮助。
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:
Hope this helps.