Jared
Jared

Reputation: 6060

Optional static value in attribute routing

I'm using attribute routing for a current project and in a few of the routes, I'm using some optional parameters. So for a URL like...

/detail/H40466/wood-to-wood-foundation-and-boxspring-frame-assembly

With its route definition like...

[Route("detail/{productName}/{applicationSlug?}")]

The wood-to-wood... is an optional parameter. What I'm wanting to do (if possible) is to have a static value only show up if the second parameter is present. Something like...

/detail/H40466/for/wood-to-wood-foundation-and-boxspring-frame-assembly

Where the word for is only part of the url when the last optional parameter is present. Is there any mechanism available to accomplish this beyond setting up another action that maps to that route?

Upvotes: 1

Views: 115

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

You can define 2 different routes for the same action method. In that case, the "optional" parameter should be required for one route and not present on the other.

[Route("detail/{productName}/for/{applicationSlug}", Order = 1)]
[Route("detail/{productName}", Order = 2)]

Upvotes: 1

Related Questions