使用asp.net MVC以编程方式重定向到404页 [英] Redirect to 404 page programmatically using asp.net MVC

查看:175
本文介绍了使用asp.net MVC以编程方式重定向到404页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Asp.net MVC应用程序.现在需要404处理.

I have created a Asp.net MVC application. Now required 404 handling.

已更新global.asax并根据状态代码显示404页面.还在web.config中添加了customErrors属性.工作正常.

Have updated global.asax and display 404 page based on status code. Also added customErrors property in web.config. Its working fine.

现在,如果有任何不符合我们要求的内容,我想以编程方式重定向到404.

Now I would like to redirect to 404 programmatically when any thing not match with our requirement.

if(!valid) 
{
    return RedirectToAction("Index", "Page404");
}

工作正常,但有2种状态,其中一种是301&然后是404.那么我该如何预防301?我只需要404.

It's working fine but there are 2 status one is 301 & then 404. So how can I prevent 301? I just need 404.

我该如何实现?

推荐答案

在您的web.config中,添加:

<customErrors mode="On" >
       <error statusCode="404" redirect="/404.shtml" />
</customErrors>

当找不到请求的资源时,它将在404.shtml页面上重定向.

This will redirect on 404.shtml page when requested resource is not found.

注意:对于这种情况,无需以编程方式重定向用户.

Note: No need to programmatically redirect users for such situation.

我从字面上建议:

if (Context.Response.StatusCode == 404) {
  // handle this
}

这篇关于使用asp.net MVC以编程方式重定向到404页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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