重定向到404页编程使用asp.net MVC [英] Redirect to 404 page programatically using asp.net MVC
问题描述
我创建了一个Asp.net MVC应用程序。现在需要404处理。
I have created a Asp.net MVC application. Now required 404 handling.
已经更新的Global.asax和显示基于状态code 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.
即。
如果(!有效)
{
返回RedirectToAction(指数,Page404);
}
i.e. if(!valid) { return RedirectToAction("Index", "Page404"); }
其工作正常,但有2个状态一个是301安培;那么404.所以,请让我知道我怎么能prevent 301.我只需要404。
Its working fine but there are 2 status one is 301 & then 404. So please let me know how can I prevent 301. I just need 404.
请让我知道我能做到这一点?
Please let me know how can I achieve this?
谢谢,
Laxmilal Menaria
Thanks, Laxmilal Menaria
推荐答案
在你的的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.
编辑:
我真的建议:
I literally suggest:
if (Context.Response.StatusCode == 404) {
// handle this
}
这篇关于重定向到404页编程使用asp.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!