如何使用标准ASP.NET MVC3网站的自定义404和500错误页面? [英] How to have a custom 404 and 500 error pages with a stock standard ASP.NET MVC3 website?
问题描述
Darin Dimitrov在这里有一个很好的SO答案,但它不适用于我所有的测试条件。
然后,广泛流行的如何在ASP.NET MVC中正确处理404?发布..但这只是触及404错误。
有人可以请说明什么
- 路线
-
web.config
设置 - 控制器/操作方法
需要做这个非常简单的事情:(
接受此答案的方案:
- VS2010 - >文件 - >新 - > ASP.NET MVC3项目/ Internet应用程序
- (右键单击解决方案).. - > U se IIS Express
- 错误页面不能是静态html页面。他们必须是一个页面,我可以像任何其他视图一样传递信息等(例如,一个
ErrorController
)...
和测试路线...
-
/首页/索引
- >显示索引页 -
/首页
- >显示索引页 -
/
- >显示索引页 -
/首页/关于
- >显示页面
-
/ asdasd / asdsad / asdas / asddasd / adsad
- > 404 -
/ adsa / asda / asd / asd / asd / asd
- > 404 -
/ asdsadasda
- > 404
然后将其添加到 HomeController .cs
class ..
public ActionResult ThrowException()
{
抛出新的NotImplementedException();
}
现在..
-
/ home / throwexception
- > 500错误
Cheers:)
BTW,一些这些真实的长途径(上)给出了非常奇怪的错误页面,当使用股票标准 new ASP.NET MVC3
模板
我将web.config文件中的customErrors指定为
< customErrors defaultRedirect =/ Error / GeneralErrormode =On>
< error statusCode =403redirect =/ Error / 403/>
< error statusCode =404redirect =/ Error / 404/>
< error statusCode =500redirect =/ Error / 500/>
< / customErrors
我在Global.asax中有一条路线,如下所示: / p>
///错误页
///
routes.MapRoute(
Error ,//路由名称
错误/ {errorCode},//带参数的URL
new {controller =Page,action =Error,errorCode = UrlParameter.Optional}
);
相应的ActionResult执行以下操作,返回相关的自定义错误页面。
//
public ActionResult错误(string errorCode)
{
var viewModel = new PageViewModel();
int code = 0;
int.TryParse(errorCode,out code);
开关(代码)
{
案例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);
案例500:
viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag(500 Internal Server Error);
return View(500,viewModel);
default:
viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag(Embarrassing Error);
return View(GeneralError,viewModel);
}
}
这允许我有许多不同的自定义错误页面。这可能不是最好的或最优雅的解决方案,但它肯定对我有用。
I'm trying to have 2 custom error pages in a stock sample ASP.NET MVC3 website.
Darin Dimitrov has a good SO answer here but it's not working for all my test conditions.
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.
Can someone please explain what
- Routes
web.config
settings- controllers / action methods
are required to do this very simple thing :(
Scenarios to accept this answer:
- 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
)...
and for the test routes...
/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
then add this to the HomeController.cs
class..
public ActionResult ThrowException()
{
throw new NotImplementedException();
}
and now ..
/home/throwexception
-> 500 error
Cheers :)
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
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
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 }
);
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.
这篇关于如何使用标准ASP.NET MVC3网站的自定义404和500错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!