加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.net MVC路由参数异常

发布时间:2020-12-16 06:24:13 所属栏目:asp.Net 来源:网络整理
导读:我有和action采取userId参数: ?/用户/显示?用户id = 1234 除非提供的userId不是int或缺失,否则一切正常. 然后它抛出此异常: Message: The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for
我有和action采取userId参数:

?/用户/显示?用户id = 1234

除非提供的userId不是int或缺失,否则一切正常.

然后它抛出此异常:

Message: The parameters dictionary contains a null entry for parameter 'userId' of 
    non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Show(Int32,System.Nullable`1[System.Boolean],System.String)' in 'S4U.Web.Mvc.Controllers.ProfileController'. An optional parameter must be a reference type,a nullable type,or be declared as an optional parameter.
        Parameter name: parameters

..之后用户被重定向到错误页面.

如何配置路由以便根本不会触发操作并且它会抛出404?

解决方法

正如我在UfukHac?o?ullar?的回答评论中所提到的那样,你应该通过向路径添加约束来处理验证,如果参数可以为空则通过可以为空的类型.

对于前一种方法,如果你有一个适当的约束,这意味着你的路线将不会被选中 – 你需要捕获所有路线或其他错误处理.对于可空类型,您可以在操作中测试它是否具有值并相应地执行操作.

将行动参数作为强类型保持是一种瞄准的模式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读