2010-08-31 99 views
0

IIS 7.5 MVC 2.0 ASP.NET 4.0MVC 2.0路線,重定向請求,請求忽略

如果我的MVC網站獲得任何不存在文件的外部請求,例如SomePage.aspx頁面有重定向這樣的請求,任何方式任何控制器/動作。

我正在使用elmah,並且在出現這種請求時拋出錯誤。

另外我也加routes.IgnoreRoute("SomePage.aspx") in global.asax.cs忽略這個請求相同,因爲我添加了忽略favicon.ico,但對於SomaPage.aspx它是行不通的,elmah無論如何拋出錯誤。

因此,有三個問題/解決方案,我會很高興得到答案:

1)如何重定向這個請求,存在控制器/動作

2)如何忽略這類請求

3)如何關閉IIS 7.5上的「驗證文件是否存在」

回答

0

您可以讓Elmah過濾掉某些類型的異常。設置它忽略404錯誤是一個相當普遍的做法:

http://code.google.com/p/elmah/wiki/ErrorFiltering

提取物:

<section name="errorFilter" type="Elmah.ErrorFilterSectionHandler, Elmah" requirePermission="false" /> 

... 

<elmah> 
    <errorFilter> 
     <test> 
      <equal binding="HttpStatusCode" value="404" type="Int32" /> 
     </test> 
    </errorFilter> 
</elmah> 
1

可以呀,,,我有一個錯誤控制器來亨德爾不同的錯誤。

在您的web.config中,添加以下內容以啓用customErrors模式,並在發生任何錯誤時將其設置爲重定向到您的控制器。

<system.web> 
    <customErrors mode="On" defaultRedirect="/error/problem"> 
     <error statusCode="404" redirect="/error/notfound" /> 
     <error statusCode="500" redirect="/error/problem" /> 
    </customErrors> 
</system.web> 

,如果你想更具體的您可以添加更多的錯誤類型上面,沒有列出任何錯誤都將回落到默認的重定向。

這是我的錯誤控制器:

public class ErrorController : Controller 
{ 

    public ActionResult NotFound() 
    { 
     ErrorViewModel model = BaseViewModelBuilder.CreateViewModel<ErrorViewModel>("We couldn't find what you were looking for"); 

     // Get the URL that caused the 404 error. 
     model.ErrorUrl = Request.UrlReferrer; 

     // Set the response status code to 404 
     Response.StatusCode = (int)HttpStatusCode.NotFound; 
     Response.TrySkipIisCustomErrors = true; 

     return View(model); 
    } 

    public ActionResult Problem() 
    { 
     ErrorViewModel model = BaseViewModelBuilder.CreateViewModel<ErrorViewModel>("Well this is embarrassing..."); 

     return View(model); 
    } 

} 

和您可能已經猜到我在錯誤視圖文件夾中的Error.aspx,Problem.aspx意見。