验证 BundleConfig 的请求 Routes/URLs

Validating Requests for BundleConfig Routes/URLs

ASP.NET 中是否有方法验证在使用 BundleConfig ScriptBundle 创建的虚拟 Paths/Routes 中发送的请求参数?

示例:

我的 BundleConfig 配置如下:

bundles.Add(new ScriptBundle("~/bundles/bjqs").Include(
                "~/Scripts/bjqs-1.3.js"));

如果用户发送如下请求:

http://example.com/bundles/bjqs?v=parameter-value_to%be+validated

如何在 ASP.NET 请求 handled/processed 之前根据正则表达式验证查询字符串 v 参数中传递的值?

您可以使用自定义 HttpModule 来拦截请求。例如:

public class MyModule1 : IHttpModule
{
    public void Dispose() {}

    public void Init(HttpApplication context)
    {
        context.AuthorizeRequest += context_AuthorizeRequest;
    }

    void context_AuthorizeRequest(object sender, EventArgs e)
    {
        var app = (HttpApplication)sender;

        // Check if the parameter is valid, your logic
        if (ValidateRequest(app.Context.Request))
        {
            // Then do nothing
            return;
        }

        // Otherwise, return unauthorized response
        app.Context.Response.StatusCode = 401;
        app.Context.Response.End();
    }
}