【问题标题】:IHttpHandler Request object and virtual directory issueIHttpHandler 请求对象和虚拟目录问题
【发布时间】:2010-03-11 03:11:06
【问题描述】:
我正在编写一个继承自 IHttpHandler 的类,用于脚本和 css 的结合。如果查询字符串定义了特殊参数,我只想合并。如果未定义此参数,那么我想编写文件的内容,就好像甚至不涉及处理程序一样。我遇到的一个问题是我在页面上有一个脚本标记,它引用了虚拟目录中的脚本,但我点击的页面位于应用程序的子目录中。
引用控制脚本的页面位于http://webserver/Admin/Default.aspx。当我在实现 IHttpHandler 的类中访问 Request 对象时,所有文件路径属性如下:webserver/Admin/~/SharedScripts/control.js。我该如何解决这个问题?
【问题讨论】:
标签:
c#
asp.net
httprequest
ihttphandler
【解决方案1】:
基本上,您需要ResolveUrl 方法,但您没有Page 或任何与此相关的控件。 This article 解释了如何在没有 Page 对象的情况下做到这一点。
【解决方案2】:
这是我想出的解决方案:
string fileContent = string.Empty;
string filePath = context.Request.PhysicalPath;
int tildeLocation = filePath.LastIndexOf("~");
string location = (tildeLocation == -1 ? filePath : context.Server.MapPath(filePath.Substring(tildeLocation, filePath.Length - tildeLocation)));