asp.net-mvc - ASP.NET MVC : requests with too long URLs and error handling

标签 asp.net-mvc iis-7 error-handling

通过检查我的 elmah 日志,我发现我不断收到“网址太长”请求。错误是:

System.Web.HttpException (0x80004005): The length of the URL for this request exceeds the configured maxUrlLength value.
   at System.Web.HttpRequest.ValidateInputIfRequiredByConfig()
   at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context)

以下是我收到的请求类型:

/index.php/blog/post/the_ten_deceptions_of_the_datetimepicker/+[PLM=0]+GET+http:/www.visualhint.com/index.php/blog/post/the_ten_deceptions_of_the_datetimepicker/+[0,23778,23037]+->+[N]+POST+http:/www.visualhint.com/index.php/blog/post/the_ten_deceptions_of_the_datetimepicker/+[0,0,2007]

(不要对 php 的事情感到惊讶......在成为 ASP.NET MVC 站点之前,我的站点是用 php 编写的,现在我需要将这种旧 URL 重定向到我的新 url 格式,这很好用当网址停止在/index.php/blog/post/the_ten_deceptions_of_the_datetimepicker 时)

  1. 什么可以生成这些请求?听起来是不是很恶意?

  2. 我有自定义错误设置,所以我虽然这样的请求会被重定向到我的自定义错误页面,但事实并非如此。相反,人们会看到典型的蓝屏(firebug 提到这是一个 400 Bad Request)。如果您查看上面的堆栈跟踪,它非常短,并且异常似乎很早就被捕获(甚至没有调用 Application_BeginRequest)。是否可以显示我的自定义错误页面,或者当发生此类异常时至少可以重定向到我的主页?

我尝试在 web.config 中添加一行错误 400:

<customErrors mode="RemoteOnly">
    <error statusCode="400" redirect="/" />
</customErrors>

这会重定向到右侧的主页,但它会在 aspxerrorpath 查询字符串值中添加完整的 url。

感谢您的帮助。

最佳答案

谷歌搜索帮助我发现其他一些人也收到了这种请求。有人回答:

I am pretty sure it is an automated way of submitting spam. There must be an error in the configuration because it should not leave such a juicy trail in the referrer field!

First it tells some script to get an URL, then it instructs to post to an URL (it is easy to block spam that POSTs directly without getting first).

The numbers could be relating to what spam message(s) to post (think of it as indexes in a spam DB).

因此,由于这些请求很可能不是来自正常链接的人类,因此我最终得到了以下解决方案。这可以避免污染我的 elmah 日志,并且通过 404 代码向调用者提供空白页面:

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    HttpException hExc = e.Exception.GetBaseException() as HttpException;
    if (hExc != null)
    {
        if (hExc.ErrorCode == unchecked((int)0x80004005))
        {
            e.Dismiss();
            return;
        }
    }

    // Here I do some stuff if the error has to be logged.
}

void Application_Error(object sender, EventArgs e)
{
    Exception exc = Server.GetLastError();
    HttpException hExc = exc as HttpException;
    if (hExc != null)
    {
        if (hExc.ErrorCode == unchecked((int)0x80004005))
        {
            Uri uri = HttpContext.Current.Request.Url;

            Response.Clear();
            Response.StatusCode = 404;
            Response.End();

            Server.ClearError();
        }
    }
}

我宁愿不发送响应(调用者会超时),而不是返回 404 代码,但是使用 asp.net 可以吗?不知道...

关于asp.net-mvc - ASP.NET MVC : requests with too long URLs and error handling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6947070/

相关文章:

error-handling - Scrape Offer 不返回任何结果

javascript - 开发库时,我们应该抛出错误/异常吗?

c# - 在 C# 的 LINQ 中按多列分组

javascript - 我们如何允许在 Dynamics CRM 中对单个字段进行多项选择?

asp.net-mvc - ServiceStack Razor - Html.RenderAction 等效项

ASP.NET 站点通过 SSL 从 Visual Studio 而不是 IIS 连接到远程

asp.net - IIS 7.0 的 ASP.NET 进程是什么?

c# - 创建使用服务的新 AuthorizationHandler/IAuthorizationRequirement

asp.net-mvc - 发送 HTTP header 后服务器无法设置状态 IIS7.5

c# - 当无法找到请求的 PostgreSQL 表时,如何捕获异常并使用错误代码?