Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC自定义错误页工作不正常_Asp.net_Asp.net Mvc_Http Status Code 404_Custom Error Pages - Fatal编程技术网

ASP.NET MVC自定义错误页工作不正常

ASP.NET MVC自定义错误页工作不正常,asp.net,asp.net-mvc,http-status-code-404,custom-error-pages,Asp.net,Asp.net Mvc,Http Status Code 404,Custom Error Pages,我正在尝试配置ASP.NET MVC自定义错误页 我添加了一个带有索引和页面未找到操作的错误控制器。我还将以下内容添加到web.config文件的部分 如果我键入URL,如http://www.example.com/Home/BadPage,我确实看到了我的错误处理程序 但是,有人能帮助我理解以下问题以及如何解决这些问题吗 在上述情况下,返回到浏览器的结果代码似乎是200而不是404。因此,像谷歌这样的爬虫程序将没有发现URL的迹象 在某些情况下,如果我的控制器操作确定URL无效,则该操作

我正在尝试配置ASP.NET MVC自定义错误页

我添加了一个带有
索引
页面未找到
操作的
错误
控制器。我还将以下内容添加到web.config文件的
部分


如果我键入URL,如http://www.example.com/Home/BadPage,我确实看到了我的错误处理程序

但是,有人能帮助我理解以下问题以及如何解决这些问题吗

  • 在上述情况下,返回到浏览器的结果代码似乎是200而不是404。因此,像谷歌这样的爬虫程序将没有发现URL的迹象
  • 在某些情况下,如果我的控制器操作确定URL无效,则该操作将返回
    HttpNotFound()
    。但发生这种情况时,将不显示“自定义错误处理程序”页面。相反,它显示了一个似乎来自IIS的通用404页面

  • 我使用的是Visual Studio和MVC的当前版本。

    除了在web.config内部进行设置外,您还需要检查异常是否为HttpException 404内部应用程序错误

    <customErrors defaultRedirect="~/Common/Error" mode="On">
      <error statusCode="404" redirect="~/Common/PageNotFound"/>
    </customErrors>
    
    公共控制器 您还希望返回
    Response.StatusCode=404内部PageNotFound操作方法

    public class CommonController : Controller
    {
        [AllowAnonymous]
        public ActionResult PageNotFound()
        {
            Response.StatusCode = 404;
            Response.TrySkipIisCustomErrors = true;
    
            return View();
        }
    
        [AllowAnonymous]
        public ActionResult Error()
        {
            Response.StatusCode = 503;
            Response.TrySkipIisCustomErrors = true;
    
            return View();
        } 
    }
    

    我正在努力让它发挥作用。两个问题:1)为什么不在错误处理程序中使用
    Response.Redirect(“/Common/PageNotFound”)
    ,以及2)当代码执行
    Response.Redirect()
    ,我认为这不会将错误代码(如404)返回到浏览器以获取原始URL。1)理想情况下,我们希望在显示404消息时在客户端浏览器中保持原始URL。Redrect导致到服务器的往返,并且不维护原始URL。2) 你可以用小提琴来测试它。哦,我明白了。您正在避免重定向。没错。
    protected void Application_Error(object sender, EventArgs e)
    {
        var exception = Server.GetLastError();
    
        // Process 404 HTTP errors
        var httpException = exception as HttpException;
        if (httpException != null && httpException.GetHttpCode() == 404)
        {
            Response.Clear();
            Server.ClearError();
            Response.TrySkipIisCustomErrors = true;
    
            IController controller = new CommonController();
    
            var routeData = new RouteData();
            routeData.Values.Add("controller", "Common");
            routeData.Values.Add("action", "PageNotFound");
    
            var requestContext = new RequestContext(
                 new HttpContextWrapper(Context), routeData);
            controller.Execute(requestContext);
        }
    }
    
    public class CommonController : Controller
    {
        [AllowAnonymous]
        public ActionResult PageNotFound()
        {
            Response.StatusCode = 404;
            Response.TrySkipIisCustomErrors = true;
    
            return View();
        }
    
        [AllowAnonymous]
        public ActionResult Error()
        {
            Response.StatusCode = 503;
            Response.TrySkipIisCustomErrors = true;
    
            return View();
        } 
    }