为什么CSS文件受全局asax中的Application_BeginRequest影响

时间:2013-10-10 16:23:04

标签: c# asp.net .net asp.net-mvc

我只想处理操作,但看起来像Application_BeginRequest方法处理CSS,图像等......

如何避免它?

因为我无法仅为操作覆盖内容:

HttpContext.Current.RewritePath("~/Maintenance.html");

2 个答案:

答案 0 :(得分:3)

如果您想限制仅对Web表单请求执行某些操作,我使用了以下内容:

protected void Application_BeginRequest(object sender, System.EventArgs e)
{
    if(HttpContext.Current.Request.CurrentExecutionFilePathExtension == ".aspx"){
         //stuff to do
    }
}

答案 1 :(得分:1)

当pp响应请求时,

HttpApplication.BeginRequest Event“作为HTTP管道执行链中的第一个事件发生。”

所以通过ASP.NET管道的任何请求都会触发这个,而不仅仅是* .aspx等。

您无法避免,但您可以检查所请求文件的路径并根据需要执行和操作,例如:

protected void Application_BeginRequest(object sender, System.EventArgs e)
{
    string file = Request.Url.LocalPath.ToLower();
    if (file == ("/user/singin"))
    {
        //something
    }
}

虽然可能还有另一种方法可以完成你的目标(你的问题并不清楚)。

相关问题