如何有一个自定义的404和500错误页面与A股标准的ASP.NET MVC3网站? [英] How to have a custom 404 and 500 error pages with a stock standard ASP.NET MVC3 website?

查看:154
本文介绍了如何有一个自定义的404和500错误页面与A股标准的ASP.NET MVC3网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个股票样本ASP.NET MVC3网站2自定义错误页。

I'm trying to have 2 custom error pages in a stock sample ASP.NET MVC3 website.

达林季米特洛夫在这里有一个很好的SO回答,但它不是努力为我所有的测试条件。

Darin Dimitrov has a good SO answer here but it's not working for all my test conditions.

再有就是广为流行的<一个href="http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/620559#620559">How我可以适当地在ASP.NET MVC处理404?后..但是这只是触及了一个404错误。

Then there is the widely popular How can I properly handle 404s in ASP.NET MVC? post .. but this just touches on a 404 error.

有人能解释什么

  • 路线
  • 的web.config 设置
  • 在控制器/ action方法
  • Routes
  • web.config settings
  • controllers / action methods

需要做到这一点很简单的事:(

are required to do this very simple thing :(

情景接受这样的回答:

  • 在VS2010 - >文件 - >新建 - > ASP.NET MVC3项目/互联网应用
  • (右键点击解决方案)。 - >使用IIS防爆preSS
  • 错误页面不能是静态的HTML页面。他们必须是我可以传递的信息来,像任何其他视图等页面(例如一个 ErrorController )...
  • VS2010 -> File -> New -> ASP.NET MVC3 project / Internet Application
  • (Right Click on the solution).. -> Use IIS Express
  • Error pages cannot be a static html page. They must be a page which I can pass info to, like any other view, etc. (eg. an ErrorController)...

和为测试路线...

  • /首页/指数 - >显示索引页
  • /首页 - >显示索引页
  • / - >显示索引页
  • /首页/关于 - >显示有关页
  • / asdasd / asdsad / asdas / asddasd / adsad - > 404
  • / ADSA / ASDA / ASD / ASD / ASD / ASD - > 404
  • / asdsadasda - > 404
  • /Home/Index -> shows index page
  • /Home -> shows index page
  • / -> shows index page
  • /Home/About -> shows about page
  • /asdasd/asdsad/asdas/asddasd/adsad -> 404
  • /adsa/asda/asd/asd/asd/asd -> 404
  • /asdsadasda -> 404

然后把它添加到 HomeController.cs 类..

then add this to the HomeController.cs class..

public ActionResult ThrowException()
{
    throw new NotImplementedException();
}

和现在..

  • /主页/ throwexception - > 500错误
  • /home/throwexception -> 500 error

干杯:)

顺便说一句,一些人真的长航线(图)给出很奇怪的错误页面,现在,使用的股票标准时,新的ASP.NET MVC3 模板

BTW, some of those realy long routes (above) give really weird error pages right now, when using the stock standard new ASP.NET MVC3 template

推荐答案

我在如下我的web.config文件中指定的customErrors;

I specify customErrors in my web.config file as below;

    <customErrors defaultRedirect="/Error/GeneralError" mode="On">
      <error statusCode="403" redirect="/Error/403" />
      <error statusCode="404" redirect="/Error/404" />
      <error statusCode="500" redirect="/Error/500" />
    </customErrors

我有如下在Global.asax的路径;

I have a route in the Global.asax as below;

            /// Error Pages
            /// 
            routes.MapRoute(
                "Error", // Route name
                "Error/{errorCode}", // URL with parameters
                new { controller = "Page", action = "Error", errorCode= UrlParameter.Optional }
            );

相应的ActionResult做以下,返回相关的自定义错误页。

The corresponding ActionResult does the following, which returns the relevant custom error page.

//
        public ActionResult Error(string errorCode)
        {
            var viewModel = new PageViewModel();

            int code = 0;
            int.TryParse(errorCode, out code);

            switch (code)
            {
                case 403:
                    viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("403 Forbidden");
                    return View("403", viewModel);
                case 404:
                    viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("404 Page Not Found");
                    return View("404", viewModel);
                case 500:
                     viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("500 Internal Server Error");
                    return View("500", viewModel);
                default:
                    viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("Embarrassing Error");
                    return View("GeneralError", viewModel);
            }
        }

这让我有很多不同的自定义错误页。这也许不是最好的或最优雅的解决方案,但它肯定对我的作品。

This allows me to have many different custom error pages. It's maybe not the best or most elegant solution, but it certainly works for me.

这篇关于如何有一个自定义的404和500错误页面与A股标准的ASP.NET MVC3网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆