9

I have the following route mapping or WEB API

[Route("Foo/Id/{id=1}/bar")]

I want to make Id optional as above however from client side no matter what I call it route doesn't match and I get 404 I try things like

Foo/Id//bar

But doesn't work. Is there way to use optional parameters with web api if the parameter is not at the end?

6
  • 2
    Another way of doing it is have 2 routes, and have your resource like: GetFooById(int id = 0). Both routes will point to that.
    – majidarif
    Commented Mar 17, 2014 at 9:18
  • 1
    I know that way, but it's not feasible if the number of optional parameters grow. In that case there will be n! routes.
    – Onur Gumus
    Commented Mar 18, 2014 at 10:58
  • Can you give more information on what you are trying to do?
    – majidarif
    Commented Mar 18, 2014 at 11:03
  • I have a method with 4 parameters optional and I want to write an attribute routing with it.
    – Onur Gumus
    Commented Mar 18, 2014 at 11:30
  • You could provide default value for the option parameter. routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "Foo/Id/{id}/bar", defaults: new { id = 0 } ); Commented Mar 24, 2014 at 0:41

5 Answers 5

0

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" });
0

use 0 instead of blank parameter and when getting 0 use it like its blank.

1
  • 1
    What if 0 is a real parameter? It doesn't make this optional
    – Onur Gumus
    Commented Mar 24, 2014 at 9:31
0

I had a similar problem and my solution was to make the route attribute nullable and pass a null argument when I did not want to provide a value. For you, this would be something like:

[Route("Foo/Id/{id:int?}/bar")]

I then handled the null condition in the logic of the controller action. I guess you could assign a default value in the controller action like so (c#):

///<summary>
/// Get my foo's bars
///</summary>
[Route("Foo/Id/{id:int?}/bar")]
public HttpResponseMessage Get(int? id)
{
    var fooId = id ?? 1; // fooId will be of type System.Int32.
    ...

So you could call either of these:

~/Foo/Id/null/bar
~/Foo/Id/5/bar

This solution works for me.

2
  • 1
    I am sure it works but its pretty ugly
    – Squibly
    Commented Jun 5, 2023 at 7:16
  • 1
    @Squibly It is ugly and I'd rather just avoid the scenario. I can't remember what I was working on at the time or what my constraints were, but the implementation isn't very RESTful. I'd try something like ~/api/foos and ~/api/foos/{id} as separate endpoints and not have 'Id' in the root at all.
    – Phil
    Commented Jun 14, 2023 at 14:15
0

Just add multiple routes to your controllers

[Route("api/[controller]")]
[Route("api/old-log")]
public class LogController: Controller
{
    [HttpGet]
    public IActionResult GetAll()
    {
        return Json(new { Foo = "bar" });
    }
}
-1

please refer to this article regarding route attribute

You could use it like this and provide defaults.

Your attribute would be looking like this:

[Route("Foo/Id/{id:int=0}/bar")]

and then handle 0 as if there was no value (as mentioned above).

1
  • 2
    I already tried it thus I asked this question. It doesn't work that way. If you add above route, Foo/Id//bar simply returns 404
    – Onur Gumus
    Commented Mar 24, 2014 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.