ASP.NET:如何从通用处理程序获取文件的虚拟路径?
如何从通用 .ashx 处理程序中将文件的虚拟路径解析为适合浏览器的路径?
例如我想转换:
~/asp/ClockState.aspx
如果
/NextAllowed/asp/ClockState.aspx
我是一个 WebForm Page
,我可以调用 ResolveUrl
:
Page.ResolveUrl("~/asp/ClockState.aspx")
解析为:
/NextAllowed/asp/ClockState.aspx
But i'm not a WebForm Page, i'm a通用处理程序。你知道,那个 IHttpHandler 对象注入了各种各样的东西:
public class ResetClock : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
//[process stuff]
//Redirect client
context.Response.Redirect("~/asp/ClockState.aspx", true);
}
public bool IsReusable { get { return false; } }
}
How can i resolve a virtual path to a file into a path, suitable for the browser, from within a generic .ashx handler?
e.g. i want to convert:
~/asp/ClockState.aspx
into
/NextAllowed/asp/ClockState.aspx
If i were a WebForm Page
, i could call ResolveUrl
:
Page.ResolveUrl("~/asp/ClockState.aspx")
which resolves to:
/NextAllowed/asp/ClockState.aspx
But i'm not a WebForm Page, i'm a generic handler. You know, that IHttpHandler
object with all kinds of things injected:
public class ResetClock : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
//[process stuff]
//Redirect client
context.Response.Redirect("~/asp/ClockState.aspx", true);
}
public bool IsReusable { get { return false; } }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 VirtualPathUtility 类来执行此操作。这包含使用路径的各种方法。您需要的是 ToAbsolute(),它将相对路径转换为绝对路径。
但是,您无论如何都可以在 Response.Redirect 调用中使用波浪号,因此以下操作仍然有效:
在使用 Response.Redirect 之前,您不需要将 URL 转换为绝对路径。
You can use the VirtualPathUtility class to do this. This contains various methods for working with paths. The one you need is ToAbsolute(), which will convert a relative path to an absolute one.
However, you can use the tilde in Response.Redirect calls anyway, so the following would still work:
You do not need to convert the URL to an absolute path before using Response.Redirect.