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

时间:2022-05-14 04:13:20

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

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

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

and what the file extension is ie. css or js

以及文件扩展名是什么。 css或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?

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

4 个解决方案

#1


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.

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

#2


        string url = context.Request.Path;
        string extension = VirtualPathUtility.GetExtension(url);

#3


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

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

#4


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'

请注意,Extension将始终具有前导'。'。例如'.css'或'.js'

#1


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.

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

#2


        string url = context.Request.Path;
        string extension = VirtualPathUtility.GetExtension(url);

#3


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

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

#4


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'

请注意,Extension将始终具有前导'。'。例如'.css'或'.js'