处理 MVC 中的无效 URL [英] Handle Invalid URL in MVC
本文介绍了处理 MVC 中的无效 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何处理 MVC 中的无效 URL?
How to handle invalid URLs in MVC?
例如:当用户输入 http://localhost/User/MyProfile 而不是http://localhost/User/Profile,会抛出异常.
For ex.: When the user enters http://localhost/User/MyProfile instead of http://localhost/User/Profile, it will throw an exception.
如何处理这个请求?
推荐答案
你需要先在web.config中添加一个自定义的Error page url:
You need first to add a custom Error page url in the web.config:
<customErrors mode="On" defaultRedirect="~/Error/404" />
并添加一个控制器来处理无效的网址:
And add a controller to handle the invalid urls:
public class ErrorController:Controller
{
[ActionName("404")]
public ActionResult Error404()
{
return View("Error");
}
}
如果你想将用户重定向到主页,那么你不需要错误控制器,只需修改自定义错误标签:
And if you want to redirect the user to the home page then you don't need the Error controller just modify the custom error tag:
<customErrors mode="On" defaultRedirect="~/Home/Index" />
这篇关于处理 MVC 中的无效 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文