本文介绍了MVC3自定义错误页面给出空白的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

使用博客发布和一个主题在SO我创建了一个控制器,它应该处理我所有的错误页面。

Using the blog posted here and a topic here on SO i've created a controller which should handle all my error pages.

在我的Global.asax.cs中,我得到以下代码:

In my Global.asax.cs I've got the following piece of code:

protected void Application_Error()
        {
            var exception = Server.GetLastError();
            var httpException = exception as HttpException;
            var routeData = new RouteData();

            Response.Clear();
            Server.ClearError();

            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = "General";
            routeData.Values["exception"] = exception;
            Response.StatusCode = 500;

            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                    case 403:
                        routeData.Values["action"] = "Http403";
                        break;
                    case 404:
                        routeData.Values["action"] = "Http404";
                        break;
                }
            }

            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
            IController errorsController = new ErrorController();
            HttpContextWrapper wrapper = new HttpContextWrapper(Context);
            var rc = new RequestContext(wrapper, routeData);
            errorsController.Execute(rc);
        }

我的ErrorController如下所示:

My ErrorController looks like this:

public class ErrorController : BaseController
    {
        /// <summary>
        /// Returns a default view for not having access.
        /// </summary>
        public ActionResult Unauthorized()
        {
            BaseModel viewModel = new BaseModel
                                      {
                                          LoginModel = new LogonModel(),
                                          ProfessionsTopX = GetTopXProfessions()
                                      };
            return View(viewModel);
        }

        public ActionResult General(Exception exception)
        {
            return View("Exception", exception);
        }

        public ActionResult Http404()
        {
            //This line works
            //return Content("Not found", "text/plain");

            //This line presents a blank page
            return View("404","_Layout");
        }

        public ActionResult Http403()
        {
            return View("403", "_Layout");
        }

    }

而我的Razor View只包含

And my Razor View only contains the piece of html below;

@{
    ViewBag.Title = "404";
}

<h2>404</h2>

This is a 404 page!

当我使用返回内容我得到一个简单的textoutput告诉我,我正在看一个404页。然而,我想要404页面适合我的设计的其余部分,所以我想使用我自己的视图。但是,一旦我使用Return View,我会得到一个空白页面。我希望失去一些非常明显的东西,但我看不到它。

When I use the Return Content i'm getting a plain textoutput telling me i'm looking at a 404-page. However, I want the 404 page to fit the rest of my design, so I want to use my own Views. However as soon as I use Return View I'm getting a blank page. I expect to be missing something very obvious, but I don't see it.

推荐答案

我有同样的问题,终于找到了适合我的解决方案。当我在 errorsController.Execute(rc); 行中放置了一个断点,并使用'step into',直到我遇到这个异常:

I was having the same problem, and finally found the solution that worked for me. The breakthrough came when I placed a breakpoint on the errorsController.Execute(rc); line, and used 'step into' until I came across this exception:

The view 'Detail' or its master was not found or no view engine supports the
searched locations. The following locations were searched:
~/Views/Errors/Detail.aspx
~/Views/Errors/Detail.ascx
~/Views/Shared/Detail.aspx
~/Views/Shared/Detail.ascx
~/Views/Errors/Detail.cshtml
~/Views/Errors/Detail.vbhtml
~/Views/Shared/Detail.cshtml
~/Views/Shared/Detail.vbhtml

异常被吞噬,我假设是因为发生在Application_Error方法内,因为我设置了 Response.TrySkipIisCustomErrors = true

The exception was being swallowed, I assume because it was happening inside the Application_Error method and because I had set Response.TrySkipIisCustomErrors = true.

看到这个错误后,我很快意识到我的问题只是一个不匹配的名称:我的控制器实际上命名为 ErrorController 没有'',而不是 ErrorsController 。我的问题是我设置了 routeData.Values [controller] =Errors; ,这是错误的。将其切换到 routeData.Values [controller] =Error; 修复了问题。

After seeing this error, I quickly realized my problem was simply one of mismatched names: My controller is actually named ErrorController with no 's', not ErrorsController. The problem for me was that I had set routeData.Values["controller"] = "Errors";, which is wrong. Switching it to routeData.Values["controller"] = "Error"; fixed the problem.

请注意,您不会立即捕获错误,因为您直接实例化了控制器,如果您的拼写错误,它将无法编译。但是在控制器内部,调用View()将使用我们构造并传递给 RequestContext RouteData 实例来查找视图>对象。因此,如果控制器名称拼写错误,则MVC不知道在哪里查找视图,并且由于IIS自定义错误被跳过,它会以静默方式失败。

Note that you won't catch the error right away, because you directly instantiate the controller, and it won't compile if you have that part spelled wrong. But inside the controller, calling View() will look for the view using the RouteData instance we constructed and passed to the RequestContext object. So if the controller name is spelled wrong there, MVC doesn't know where to look for the view, and since IIS custom errors are skipped, it fails silently.

长故事简介:检查您的控制器并查看名称。如果您的控制器名称正确,但视图的文件名不匹配,我会发生类似的事情。

Long story short: Check your controller and view names. I assume something similar would happen if you have the controller name correct, but the file name of the view doesn't match.

这篇关于MVC3自定义错误页面给出空白的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:22