处理MVC URL无效 [英] Handle Invalid URL in MVC

查看:132
本文介绍了处理MVC URL无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理无效的网址,在MVC?

How to handle invalid URLs in MVC?

有关例:当用户进入的http://的本地主机/用户/我的资料,而不是
HTTP://本地主机/用户/档案,它会抛出一个异常

For ex.: When the user enters http://localhost/User/MyProfile instead of http://localhost/User/Profile, it will throw an exception.

如何处理这个请求?

推荐答案

您需要先在web.config中添加自定义错误页的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屋!

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