IHttpHandler请求对象和虚拟目录问题

发布于 2024-08-24 21:07:51 字数 407 浏览 3 评论 0原文

我正在编写一个继承自 IHttpHandler 的类,用于脚本和 css 组合。我只想在查询字符串定义了特殊参数时进行组合。如果未定义此参数,那么我想写入文件的内容,就好像处理程序根本不参与一样。我遇到的一个问题是,我在页面上有一个脚本标记,该标记引用虚拟目录中的脚本,但我正在访问的页面位于应用程序的子目录中。

引用控制脚本的页面位于 http://webserver/Admin/Default.aspx。当我访问实现 IHttpHandler 的类中的 Request 对象时,所有文件路径属性如下:webserver/Admin/~/SharedScripts/control.js。我该如何解决这个问题?

I am writing a class that inherits from IHttpHandler for script and css combining. I only want to combine if the querystring has a special parameter defined. If this parameter is not defined then I want to write the contents of the file as if the handler wasn't even involved. The one problem I'm encountering is that I have a script tag on a page that refers to a script in a virtual directory but the page I am hitting is in a subdirectory of the application.

The page that the control script is being referenced from is located at http://webserver/Admin/Default.aspx. When I access the Request object in the class that implements IHttpHandler all file path properties are the following: webserver/Admin/~/SharedScripts/control.js. How do I resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2024-08-31 21:07:51

基本上,您需要 ResolveUrl 方法,但没有 Page 或任何与此相关的控件。 这篇文章解释了如何在没有Page<的情况下做到这一点/code> 手头的物体。

Basically, you need the ResolveUrl method, but you don't have a Page, or any controls for that matter. This article explains how to do that without a Page object at hand.

花海 2024-08-31 21:07:51

这是我想出的解决方案:

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)));

This is the solution I came up with:

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)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文