HttpModule/HttpApplication 测试 url 是否为文件请求

发布于 2024-07-29 07:35:10 字数 227 浏览 5 评论 0原文

在 HttpModule 中,我想检查 url 是否以文件结尾:

即。 www.example.com/images/images.css

以及文件扩展名是什么,即。 。

在 Begin_Request 事件处理程序中,使用嵌套在 HttpApplication 中的 Request 对象的 Url 属性,我当前正在使用 String 操作来剪切文件扩展名 有一个更好的方法吗?

Within a HttpModule I would like to check whether the url ends with a file:

ie. www.example.com/images/images.css

and what the file extension is ie. css or js

In the Begin_Request event handler, using the Url property of the Request object nested in the HttpApplication, I am currently cutting of the file extension using String operations. Is there a better way to do this?

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

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

发布评论

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

评论(4

爱情眠于流年 2024-08-05 07:35:10

下面的代码应该会为您提供所请求文件的扩展名。

private void context_BeginRequest(object sender, EventArgs e)
{
    HttpApplication application = (HttpApplication)sender;
    HttpContext context = application.Context;

    string ext = System.IO.Path.GetExtension(context.Request.Path);
    // ext will always start with dot

}

但与 .aspx 和 .ashx 等文件类型不同,您在示例中使用的 .js 和 .css 等文件类型默认情况下不会在 IIS 中向 ASP.Net dll 注册,因此当请求它们时 IIS 不会通过通过 ASP.Net 管道发送请求,因此不会运行 HttpModules 或 HttpHandlers。 如何配置此操作取决于您运行的 IIS 版本。

The code below should get you the extension for the requested file.

private void context_BeginRequest(object sender, EventArgs e)
{
    HttpApplication application = (HttpApplication)sender;
    HttpContext context = application.Context;

    string ext = System.IO.Path.GetExtension(context.Request.Path);
    // ext will always start with dot

}

But unlike file types such as .aspx and .ashx file types such as .js and .css that you have used in your example are not by default registered with the ASP.Net dll within IIS so when they are requested IIS doesn't pass the request through the ASP.Net pipeline so no HttpModules or HttpHandlers will run. How you configure this to happen depends on what version of IIS you are running on.

孤独患者 2024-08-05 07:35:10
        string url = context.Request.Path;
        string extension = VirtualPathUtility.GetExtension(url);
        string url = context.Request.Path;
        string extension = VirtualPathUtility.GetExtension(url);
痕至 2024-08-05 07:35:10

请查看 HttpRequest.Url 的属性。 它的类型为 System.Uri< /a>.

Please look at the properties of HttpRequest.Url. It is of the type System.Uri.

可是我不能没有你 2024-08-05 07:35:10

试试这个:

// get the URI
Uri MyUrl = Request.Url; 
// remove path because System.IO.Path doesn't like forward slashes
string Filename = MyUrl.Segments[MyUrl.Segments.Length-1]; 
// Extract the extension
string Extension = System.IO.Path.GetExtension(Filename); 

请注意,Extension 始终以“.”开头。 例如“.css”或“.js”

Try this:

// get the URI
Uri MyUrl = Request.Url; 
// remove path because System.IO.Path doesn't like forward slashes
string Filename = MyUrl.Segments[MyUrl.Segments.Length-1]; 
// Extract the extension
string Extension = System.IO.Path.GetExtension(Filename); 

Note that Extension will always have the leading '.'. e.g. '.css' or '.js'

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