MVC 2.0 Routes, redirect request, ignore request
IIS 7.5 MVC 2.0 ASP.NET 4.0
If my MVC site getting external req开发者_如何学Cuest for any not existed files, e.g SomePage.aspx is there any ways to redirect such request to any controller/action.
I am using elmah and it is throwing errors when such kind of requests coming.
Also i did add routes.IgnoreRoute("SomePage.aspx") in global.asax.cs
to ignore this requests the same as i add to ignore favicon.ico but for SomaPage.aspx it is won't work, elmah throwing errors anyway.
So there is three questions/solutions i would be happy to get the answers:
1) How to redirect this request to existed controller/action
2) How to Ignore such kind of requests
3) How to turn off "Verify that file exists" on IIS 7.5
Yes you can ,,, I have an Error Controller to Handel different errors.
In you web.config add the following to enable customErrors mode and set it to redirect to your controller when any errors occurs.
<system.web>
<customErrors mode="On" defaultRedirect="/error/problem">
<error statusCode="404" redirect="/error/notfound" />
<error statusCode="500" redirect="/error/problem" />
</customErrors>
</system.web>
You can add more error types to the above if you want to be more specific, any errors that is not listed will fall back to the default redirect.
and here's my Error Controller :
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);
}
}
and as you might guess I have the Error.aspx, Problem.aspx views in the Error view folder.
You can get Elmah to filter out certain types of exception. Setting it up ignore 404 errors is a fairly common practice:
http://code.google.com/p/elmah/wiki/ErrorFiltering
Extract:
<section name="errorFilter" type="Elmah.ErrorFilterSectionHandler, Elmah" requirePermission="false" />
...
<elmah>
<errorFilter>
<test>
<equal binding="HttpStatusCode" value="404" type="Int32" />
</test>
</errorFilter>
</elmah>
精彩评论