Asp.net MVC Controller接受值或null

Kle*_*rBH 7 c# asp.net asp.net-mvc asp.net-mvc-4

我有一个像这样的控制器:

public ActionResult mycontroller(int status, DateTime start, DateTime end) 
{
   ...stuff
}
Run Code Online (Sandbox Code Playgroud)

我称之为:

http://localhost:22200/mycontroller?status=1&start=18/03/2015&end=18/04/2015
Run Code Online (Sandbox Code Playgroud)

但是,有一半的时间我需要像这样调用它:

http://localhost:22200/mycontroller?status=1
Run Code Online (Sandbox Code Playgroud)

我想为两个电话使用相同的控制器.

有没有办法改变控制器接受或不接受参数?

Ps:我不想拥有2个控制器或这样的网址

http://localhost:22200/mycontroller?status=1&start=null&end=null
Run Code Online (Sandbox Code Playgroud)

有帮助吗?非常感谢.

Chr*_*ter 9

Make startendoptional/nullable参数如下图所示.

ActionResult Action(int status, DateTime? start = null, DateTime? end = null)

可选的参数将允许采取行动,无论是否将被调用startend查询字符串指定的参数.省略时,null将使用默认值.因此,以下两个URL都有效:

HTTP://本地主机:22200 /状态myController的= 1

HTTP://本地主机:22200 /状态myController的= 1&开始= 18/03/2015&端= 18/04/2015

  • 仅供参考,默认的`= null`不是必需的.必要的是可以为空的参数`DateTime?`而不是不可为空的`DateTime`. (5认同)