MVC 2.0 Routes, redirect request, ignore request

纵然是瞬间 提交于 2019-12-11 16:35:09

问题


IIS 7.5 MVC 2.0 ASP.NET 4.0

If my MVC site getting external request 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


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/3605703/mvc-2-0-routes-redirect-request-ignore-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!