失踪asp.net的MVC参数时自动抛出404错误 [英] Automatically throw 404 errors when missing parameters for asp.net mvc

查看:127
本文介绍了失踪asp.net的MVC参数时自动抛出404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误很多,当谷歌机器人自带方式:


  

参数词典共包含参数非可空类型'System.Int32'的'身份证'的方法'System.Web.Mvc.ActionResult展(Int32)已'在'SomeClass的'空项。一个可选参数必须是引用类型,可空类型,或声明为可选参数。结果
  参数名:参数


我不知道是否有可能让应用程序抛出404的,而不是在这种情况下,缺少的参数例外。

谢谢!

更新,以澄清什么,我想要的是所有情况下,此特定错误写一些,只有抓住这个错误的包装抛出一个404错误,而不是500 preferably。


解决方案

 公众的ActionResult指数(INT?ID)
{
   如果(!id.HasValue())
   {
     抛出新的HttpException(404,你确定你在正确的地方是?);
   }
}

I'm getting the following error a lot when the Google bot comes by:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Show(Int32)' in 'someclass'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

I was wondering if it would be possible to have the application throw 404's instead of missing parameter exception in this case.

Thanks!

Update to clarify what I want is that all cases for this particular error throw a 404 error instead of a 500. Preferably by writing a wrapper of some kind that only catches this error.

解决方案

public ActionResult Index(int? id)
{
   if(!id.HasValue())
   {
     throw new HttpException(404, "Are you sure you're in the right place?");
   }
}

这篇关于失踪asp.net的MVC参数时自动抛出404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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