IIS 7.5 MVC 2.0 ASP.NET 4.0
如果我的MVC网站获得任何不存在的文件的外部请求,例如SomePage.aspx有任何方法可以将此类请求重定向到任何控制器/操作。
我正在使用elmah,当发出此类请求时,它会抛出错误。
此外,我确实添加了routes.IgnoreRoute("SomePage.aspx") in global.asax.cs
来忽略此请求,就像我添加忽略favicon.ico一样,但是对于SomaPage.aspx它将无法正常工作,无论如何都会抛出错误。
所以有三个问题/解决方案我很乐意得到答案:
1)如何将此请求重定向到现有的控制器/操作
2)如何忽略此类请求
3)如何在IIS 7.5上关闭“验证文件是否存在”
答案 0 :(得分:1)
是的你可以,,,我有一个错误控制器来处理Handel不同的错误。
在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视图文件夹中有Error.aspx,Problem.aspx视图。
答案 1 :(得分: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>