Asp.net MVC 控制器接受值或 null
Asp.net MVC Controller to accept value or null
我有这样的控制器:
public ActionResult mycontroller(int status, DateTime start, DateTime end)
{
...stuff
}
我这样称呼它:
http://localhost:22200/mycontroller?status=1&start=18/03/2015&end=18/04/2015
然而,有一半的时间我需要这样称呼它:
http://localhost:22200/mycontroller?status=1
我想对两个调用使用相同的控制器。
有没有办法改变控制器接受或不接受参数?
Ps:我不想有 2 个控制器或像这样的 url
http://localhost:22200/mycontroller?status=1&start=null&end=null
有什么帮助吗?
非常感谢。
使 start
和 end
optional/nullable 参数如下图所示。
ActionResult Action(int status, DateTime? start = null, DateTime? end = null)
可选参数将允许调用操作,无论是否指定了 start
和 end
查询字符串参数。省略时,将使用默认值 null
。因此,以下两个 URL 都是有效的:
http://localhost:22200/mycontroller?status=1
http://localhost:22200/mycontroller?status=1&start=18/03/2015&end=18/04/2015
我有这样的控制器:
public ActionResult mycontroller(int status, DateTime start, DateTime end)
{
...stuff
}
我这样称呼它:
http://localhost:22200/mycontroller?status=1&start=18/03/2015&end=18/04/2015
然而,有一半的时间我需要这样称呼它:
http://localhost:22200/mycontroller?status=1
我想对两个调用使用相同的控制器。
有没有办法改变控制器接受或不接受参数?
Ps:我不想有 2 个控制器或像这样的 url
http://localhost:22200/mycontroller?status=1&start=null&end=null
有什么帮助吗? 非常感谢。
使 start
和 end
optional/nullable 参数如下图所示。
ActionResult Action(int status, DateTime? start = null, DateTime? end = null)
可选参数将允许调用操作,无论是否指定了 start
和 end
查询字符串参数。省略时,将使用默认值 null
。因此,以下两个 URL 都是有效的:
http://localhost:22200/mycontroller?status=1
http://localhost:22200/mycontroller?status=1&start=18/03/2015&end=18/04/2015