Routing a few optional parameters

I have the following route definition in my webapi project. I have a problem, one of the parameters is not passed. eg,

when I call / Controller / Action / param 2 / startdate / enddate the value passed for param2 is taken for param1 and vice versa. The problem is that the RoutingModule cannot detect that the provided route value for param2 is not param1

It works if I use querystring in the url but don't want to use querystring. Appreciate your help.

Is there any way to achieve what I expect?

config.Routes.MapHttpRoute(
    name: "RetrieveHistory",
    routeTemplate: "{controller}/{action}/{param1}/{param2}/{startDate}/{endDate}",
    defaults: new
    {
        controller = "Vend",
        action = "RetrieveUtrnHistory",
        param1 = RouteParameter.Optional,
        param2 = RouteParameter.Optional,
        starDate = RouteParameter.Optional,
        endDate = RouteParameter.Optional
    });

thank

+5
source share
2 answers

, :

  • . , URL-, .
  • - slash / ,
  • , , ,
  • , , , ( MVC, )

, URL-, .

, TestController Web API Controller :

// GET api/Test/TestAction/ ...
[HttpGet]
public object TestAction(int param1, DateTime startDate, DateTime endDate, 
                         int? param2 = null)
{
    return new
    {
        param1 = param1,
        param2 = param2,
        startDate = startDate,
        endDate = endDate
    }.ToString();
}

. HTTP GET Web API GetXxx, HTTP POST .. PostXxx. , Controller Action URL, [HttpXxx], HTTP.

, param1 param2 , stardDate endDate :

http://myhost/api/Mycontroller/Myaction/12/22/2014-12-01/2014-12-31
http://myhost/api/Mycontroller/Myaction/22/2014-12-01/2014-12-31

, URL- :

param1 = 12; param2 = 22; startDate = 2014-12-01; endData = 2014-12-31

:

param1 = 12; param2 = null; startDate = 2014-12-01; endData = 2014-12-31

, URL, ..

// for the 1st
routeTemplate: "api/{controller}/{action}/{param1}/{param2}/{startDate}/{endDate}"
// for the 2nd
routeTemplate: "api/{controller}/{action}/{param1}/{startDate}/{endDate}"

, , .. URL- , .

, URL- param2, TestAction . : , :

  • int? param2 = null ( # , ).
  • : defaults: new { param2 = RouteParameter.Optional }

. , , , -API.

. , MVC ,

:

  • ,
  • , URL-, . , URL , .

constraint , :

config.Routes.MapHttpRoute(
    name: "Multiparam2",
    routeTemplate: "api/{controller}/{action}/{param1}/{param2}/{startDate}/{endDate}",
    constraints: new
    {
        startDate = @"20\d\d-[0-1]?\d-[0-3]?\d", // regex
        endDate = @"20\d\d-[0-1]?\d-[0-3]?\d" // regex
    },
    defaults: new object { }
);

, defaults, .

. , 20XX, , , 0x 1x, 0x, 1x, 2x 3x, . , 2012-1-1 2015-12-30, 1920-12-30. .

, (), .

, URL-, , , . ( : ).

, param2 startDate endDate, .

:

[HttpGet]
public object TestAction(int param1, int? param2 = null, DateTime? startDate = null, 
                         DateTime? endDate = null)
{
    return new
    {
        param1 = param1,
        param2 = param2,
        startDate = startDate,
        endDate = endDate
    }.ToString();
}



config.Routes.MapHttpRoute(
    name: "Multiparam1",
    routeTemplate: "api/{controller}/{action}/{param1}/{startDate}/{endDate}",
    constraints: new
    {
        startDate = @"20\d\d-[0-1]?\d-[0-3]?\d",
        endDate = @"20\d\d-[0-1]?\d-[0-3]?\d"
    },
    defaults: new
    {
        param2 = RouteParameter.Optional,
        startDate = RouteParameter.Optional,
        endDate = RouteParameter.Optional
    }
);

config.Routes.MapHttpRoute(
    name: "Multiparam2",
    routeTemplate: "api/{controller}/{action}/{param1}/{param2}/{startDate}/{endDate}",
    constraints: new
    {
        startDate = @"(20\d\d-[0-1]?\d-[0-3]?\d)?",
        endDate = @"(20\d\d-[0-1]?\d-[0-3]?\d)?"
    },
    defaults: new
    {
        startDate = RouteParameter.Optional,
        endDate = RouteParameter.Optional
    }
);

, :

  • , , . Multiparam2, URL: http://localhost:1179/api/test/testaction/1/2014-12-12/2015-1-1, param1=1; param2="2014-12-12"; startDate="2015-1-1". ( param2, , param2=@"\d+")
  • startDate endDate.

:

  • ,

, , , , .

+13

JotaBe . , , routeTemplate .

:

// for the 1st
routeTemplate: "api/{controller}/{action}/{param1}/{startDate}/{endDate}"

// for the 2nd
routeTemplate: "api/{controller}/{action}/{param1}/{param2}/{startDate}/{endDate}"
0

Source: https://habr.com/ru/post/1569319/


All Articles