我在控制器中有一个方法:
public ActionResult SendNotification(Guid? id=null, SendNotification notification =null)
Run Code Online (Sandbox Code Playgroud)
它响应 /Apps/SendNotification/{guid}?{SendNotification properties}
SendNotification是具有多个字符串属性的模型.
我的问题是SendNotification永远不会为空.无论我如何调用该操作,.NET似乎总是实例化SendNotification的对象(所有字段都为null).
我甚至与测试System.Web.Http.FromUri,并System.Web.Http.FromBody和它仍然做到这一点.
有任何想法吗?
注意:这不是WebApi.这是常规的MVC.
我的项目只有默认路由:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
那是模型绑定在欺骗你。
由于它找不到任何参数来绑定到模型,因此所有属性均为 null,但默认情况下仍会实例化模型。
不要检查模型是否为空,而是检查主要属性之一(例如 ID)是否为空。
| 归档时间: |
|
| 查看次数: |
178 次 |
| 最近记录: |