在Application_BeginRequest期间检查静态文件? [英] Check for a static file during Application_BeginRequest?

查看:221
本文介绍了在Application_BeginRequest期间检查静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Global.asx文件,该文件需要执行自定义身份验证,审核和配置文件.之所以需要这样做是因为它支持基于SAML的SSO系统,并且需要覆盖普通的.Net身份验证(不支持SAML或混合身份验证)

I have a Global.asx file that needs to do custom authentication, auditing and profiling stuff. This is needed because it supports a SAML based SSO system and needs to override the normal .Net authentication (which doesn't support either SAML or mixed authentication)

我不想为静态文件(例如.js.css.png等)触发它

I don't want to fire it for static files, such as .js, .css, .png, etc

在Cassini/WebDev和IIS7中确实如此.

In Cassini/WebDev and IIS7 it does.

我想要做的是一些简单的检查,例如this.Request.IsStaticFile(不幸的是,它不存在)以标识静态文件.

What I want to have is some simple check, like a this.Request.IsStaticFile (which doesn't exist, unfortunately) to identify the static files.

我意识到这写起来很简单,但是感觉必须已经存在-IIS已经为静态文件应用了缓存策略,等等.

I realise that this would be fairly simple to write, but it feels like something that must already exist - IIS has already applied caching policy stuff for the static files and so on.

我需要一种代码解决方案,而不是IIS配置更改之一.

I need a code solution, rather than an IIS config change one.

更新

这是我当前的解决方法:

This is my current workaround:

/// <summary>Hold all the extensions we treat as static</summary>
static HashSet<string> allowedExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
    ".js", ".css", ".png", ...
};

/// <summary>Is this a request for a static file?</summary>
/// <param name="request">The HTTP request instance to extend.</param>
/// <returns>True if the request is for a static file on disk, false otherwise.</returns>
public static bool IsStaticFile(this HttpRequest request)
{
    string fileOnDisk = request.PhysicalPath;
    if (string.IsNullOrEmpty(fileOnDisk))
    {
        return false;
    }

    string extension = Path.GetExtension(fileOnDisk);

    return allowedExtensions.Contains(extension);
}

这有效并且足够快,但是感觉很笨拙.如果我们添加未想到的新静态文件,尤其是依赖扩展名将容易出错.

This works and is quick enough, but feels horribly clunky. In particular relying on extensions is going to be error prone if we add new static files not thought of.

是否有一种更好的方法而不更改IIS配置?

Is there a better way without changing the IIS config?

推荐答案

毫无疑问,您需要创建自定义扩展方法,因为ASP.NET路由引擎使用此代码来确定文件是否存在,

There is no doubt that you need to create a custom extension method because ASP.NET routing engine uses this code to decide whether a file exist,

if (!this.RouteExistingFiles)
{
    string appRelativeCurrentExecutionFilePath = httpContext.Request.AppRelativeCurrentExecutionFilePath;
     if (((appRelativeCurrentExecutionFilePath != "~/") && (this._vpp != null)) && (this._vpp.FileExists(appRelativeCurrentExecutionFilePath) || this._vpp.DirectoryExists(appRelativeCurrentExecutionFilePath)))
     {
          return null;
       }
}

您将无法使用context.handler来确定Application_BeginRequest中的请求是否静态,因为路由模块可能会更改处理程序,并且该模块始终在Application_BeginRequest之后执行.我的建议是使用ASP.NEt路由引擎使用的类似代码.

You will not able to decide whether the request is static in Application_BeginRequest using context.handler because Routing Module may change the handler and this module always execute after Application_BeginRequest. My suggestion is to use the similar code which ASP.NEt routing engine uses.

这篇关于在Application_BeginRequest期间检查静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆