Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 我如何重定向到";“未找到页面”;使用MVC3?_Asp.net Mvc_Model View Controller_Asp.net Mvc 3_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc 我如何重定向到";“未找到页面”;使用MVC3?

Asp.net mvc 我如何重定向到";“未找到页面”;使用MVC3?,asp.net-mvc,model-view-controller,asp.net-mvc-3,asp.net-mvc-routing,Asp.net Mvc,Model View Controller,Asp.net Mvc 3,Asp.net Mvc Routing,我在谷歌上有一些链接,我现在不希望再使用了。所有链接如下所示: www.abc.com/xx/que=xxxxxxxxxxx 其中x可以是任何东西 有人能告诉我如何设置路由和控制器操作,将404返回到谷歌吗?我想我需要设置一个包含“que”的面具,但我不太确定如何做到这一点 将新路线添加到global.asax的顶部。这将捕获形式为xx/que={anything}的请求,使用正则表达式定义“que”参数 routes.MapRoute( "PageNotFound", "x

我在谷歌上有一些链接,我现在不希望再使用了。所有链接如下所示:

www.abc.com/xx/que=xxxxxxxxxxx
其中x可以是任何东西

有人能告诉我如何设置路由和控制器操作,将404返回到谷歌吗?我想我需要设置一个包含“que”的面具,但我不太确定如何做到这一点

将新路线添加到global.asax的顶部。这将捕获形式为
xx/que={anything}
的请求,使用正则表达式定义“que”参数

routes.MapRoute(
    "PageNotFound", 
    "xx/{que}",
    new { controller = "Error", action = "NotFound" },
    new { que = "que=.*" });
这还假设您在/Views/Error/目录中有一个带有action
NotFound
ErrorController
和名为
NotFound.aspx
的对应视图

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = 404;
        return View();
    }
}

我是否可以使用return RedirectPermanent之类的等价物来代替返回错误视图?@Timothy当然可以只返回
RedirectPermanent(字符串url)
而不是NotFound视图。所以,如果你想重定向到谷歌,你可以做
returnredirectpermanent(“http://www.google.com");抛出新的HttpException(404,“你的错误消息”),但您应该从站点返回一些内容。因此,使用我的答案并将Response.StatusCode设置为404(我将编辑我的答案),这将提供更好的用户体验。