简体   繁体   English

ASP.NET WebAPI路由参数

[英]ASP.NET webapi route parameters

I'm trying to make an action with 2 parameters, 1 is optional I'm trying with 我正在尝试使用2个参数进行操作,其中1个是可选操作

 [HttpGet, Route("movies/date/{dateMin}&{dateMax}")]

But it's not working. 但这不起作用。 'dateMax' is optional parameter, and when it's not given it should be the same value as dateMin Already tried with 'dateMax'是可选参数,如果未指定,则该值应与dateMin相同。

 [HttpGet, Route("movies/date/{dateMin}&{dateMax?}")]

But it's not working either. 但这也不起作用。 I dont want to have something like 我不想有类似的东西

{dateMin}/{dateMax}

Is there other possibility to do that? 还有其他可能性吗?

You should be doing that in your RouteConfig.cs 您应该在RouteConfig.cs中这样做

 routes.MapRoute(
        name: "Movies",
        url: "{controller}/{action}/{dateMin}/{dateMax}",
        defaults: new { controller = "movies", action = "date", dateMax= UrlParameter.Optional }
        );

Your Route should be like this 您的路线应该是这样

"{controller}/{action}/{dateMin}/{dateMax}"

You need to segregate the route parameters in your route using a slash and not using the query string notation ( & ). 您需要使用斜线而不是使用查询字符串符号( & )分隔路由中的路由参数。

[HttpGet, Route("movies/date/{dateMin}/{dateMax?}")]
public IHttpActionResult MoviesDate(DateTime dateMin, DateTime? dateMax){
}

There is no need to change the route config if you use RoutAttribute 如果使用RoutAttribute,则无需更改路由配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM