user6830335
user6830335

Reputation:

ASP.NET webapi route parameters

I'm trying to make an action with 2 parameters, 1 is optional I'm trying with

 [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

 [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?

Upvotes: 0

Views: 1151

Answers (2)

Igor
Igor

Reputation: 62308

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

Upvotes: 1

Aravind
Aravind

Reputation: 41581

You should be doing that in your 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}"

Upvotes: 1

Related Questions