Optional Parameter in the Middle of a Route

Optional parameter in the middle of a route

No. Optional parameters need to go to the end of the route, otherwise Router wouldn't know how to match URLs to routes. What you implemented already is the correct way of doing that:

get('things/entities', 'MyController@doSomething');
get('things/{id}/entities', 'MyController@doSomething');

You could try doing it with one route:

get('things/{id}/entities', 'MyController@doSomething');

and pass * or 0 if you want to fetch entities for all things, but I'd call it a hack.

There are some other hacks that could allow you to use one route for that, but it will increase the complexity of your code and it's really not worth it.

Web Api Optional Parameters in the middle with attribute routing

Optional parameters must be at the end of the route template. so what you are trying to do is not possible.

Attribute routing: Optional URI Parameters and Default Values

you either change your route template

[Route("Calls/{id:int?}/{callId:int?}")]

or create a new action

[RoutePrefix("api/Employees")]
public class CallsController : ApiController {

//GET api/Employees/1/Calls
//GET api/Employees/1/Calls/1
[HttpGet]
[Route("{id:int}/Calls/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int id, int? callId = null) {
var testRetrieve = id;
var testRetrieve2 = callId;

throw new NotImplementedException();
}

//GET api/Employees/Calls
[HttpGet]
[Route("Calls")]
public async Task<ApiResponse<object>> GetAllCalls() {
throw new NotImplementedException();
}
}

Web API Optional parameters in the middle

You could not use // in URL.
The only way to make things you want is to map 2 routes for one endpoint.
For examle

config.Routes.MapHttpRoute(
name: "BarRoute",
routeTemplate: "Foo/Id/{id}/bar",
defaults: new { controller = "Foo" });

config.Routes.MapHttpRoute(
name: "BarDefaultRoute",
routeTemplate: "Foo/Id/bar",
defaults: new { controller = "Foo" });

Angular 2 optional route parameter

You can define multiple routes with and without parameter:

@RouteConfig([
{ path: '/user/:id', component: User, name: 'User' },
{ path: '/user', component: User, name: 'Usernew' }
])

and handle the optional parameter in your component:

constructor(params: RouteParams) {
var paramId = params.get("id");

if (paramId) {
...
}
}

See also the related github issue: https://github.com/angular/angular/issues/3525

Multiple optional route parameters in Express?

The expressjs's guide to routing mentions:

Express uses path-to-regexp for matching the route paths; see the
path-to-regexp documentation for all the possibilities in defining
route paths. Express Route Tester is a handy tool for testing basic
Express routes, although it does not support pattern matching.

Basically, you can use the ? character to make the parameter optional.

/articles/:year?/:month?/:day?

Azure function optional middle route parameter

As you have found, Azure function doesn't support optional “middle” route parameter yet. Only consecutive optional parameter like SomeRoute/{MyOptionalRoute:int?}/{AnotherRoute:int?} works.

Back to the point, find a workaround with proxy for function, see whether it meets your requirement.

Add a proxies.json to your function project, change file property copy to output directory to copy if newer.

Sample Image

See content below, I use 0 as the reserved number as the alternative of null value. Proxy directs http://localhost/api/SomeRoute/AnotherRoute to the real url http://localhost/api/SomeRoute/0/AnotherRoute, which matches the pattern of SomeRoute/{MyOptionalRoute:int}/AnotherRoute.

{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"proxy1": {
"matchCondition": {
"methods": [ "GET" ],
"route": "/api/SomeRoute/AnotherRoute"
},
"backendUri": "http://localhost/api/SomeRoute/0/AnotherRoute"
}
}
}


Related Topics



Leave a reply



Submit