簡體   English   中英

Asp.Net MVC:請求URL過長和錯誤處理

[英]Asp.Net MVC: requests with too long URLs and 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中,現在我需要將這種舊網址重定向到我的新網址格式,這在網址上運行良好停在/index.php/blog/post/the_ten_deceptions_of_the_datetimepicker)

  1. 什么可以產生這些要求? 聽起來有惡意嗎?

  2. 我有自定義錯誤設置,所以我雖然這樣的請求會被重定向到我的自定義錯誤頁面,但事實並非如此。 相反,人們得到典型的黃色屏幕(螢火蟲提到這是一個400 Bad Request)。 如果你看一下上面的堆棧跟蹤,它很短,異常似乎很早就被捕獲(甚至沒有調用Application_BeginRequest)。 是否可以顯示我的自定義錯誤頁面,或者當發生此類異常時,我是否至少可以重定向到我的主頁?

我嘗試在web.config中為錯誤400添加一行:

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

這會重定向到主頁右側,但它會在aspxerrorpath查詢字符串值中添加完整的URL。

謝謝你的幫助。

谷歌搜索幫助我發現其他人得到了這種請求。 有人回答:

我很確定這是一種自動提交垃圾郵件的方式。 配置中必定存在錯誤,因為它不應該在referrer字段中留下如此多汁的蹤跡!

首先它告訴一些腳本獲取一個URL,然后它指示發布到一個URL(很容易阻止直接POST的垃圾郵件而不先獲取)。

這些數字可能與要發布的垃圾郵件有關(將其視為垃圾郵件數據庫中的索引)。

因此,由於很有可能這些請求不是來自普通鏈接的人類,我最終得到了以下解決方案。 這樣可以避免污染我的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嗎? 不知道...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM