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
.
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
How to Get a PHP Value from an HTML Form
PHP Eval That Evaluates HTML & PHP
Php: Variable-Length Argument List by Reference
Upload Xls or Xlsx Files with Codeigniter, Mime-Type Error
How to Set the Value of a Textbox Through PHP
Add a Shipping to an Order Programmatically in Woocommerce 3
How to Ensure I Caught All Errors from MySQLi::Multi_Query
How to Iterate by Row Through a MySQL Query in PHP
Sort Array by Value Alphabetically PHP
Phpmailer Attachment, Doing It Without a Physical File
How to Solve "Non-Static Method Xxx:Xxx() Should Not Be Called Statically in PHP 5.4
Call Parent Constructor Before Child Constructor in PHP
Run PHP Function Inside Jquery Click
PHP Function to Replace a (I)Th-Position Character
Mysqli Insert Error Incorrect Syntax
Laravel Access Denied for User 'Root'@'Localhost' (Using Password: Yes) in Laravel 4.2