8

With the new MVC Attribute routing, I know you can assign multiple Route attributes to a single ActionResult, but I am looking for a way to do the same at the RoutePrefix level. I have a controller which, in every action, should be accessible by three routes:

/Games/{Title}/Characters/{Route} /Books/{Title}/Characters/{Route} /Cinema/{Title}/Characters/{Route}

I tried putting three individual RoutePrefix attributes, but I get the error Deuplace RoutePrefix attribute. If I try to use a comma-separated list, I get Best override method for does not contain a constructor that takes 3 arguments.

Is it possible to set up a RoutePrefix so that it takes the three routes I want for my controller?

alex
  • 6,818
  • 9
  • 52
  • 103
Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123

1 Answers1

16

Running a bunch of tests I found out that I can just add 3 Route attributes to the controller level and it works the way I want.

Edit: a better way to do it I found was using the regex match method

[RoutePrefix("{Type:regex(Games|Cinema|Books)}/{SectionRoute}/Character/")]

Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
  • How would you do it without a regex? Can I just add multiple attribute lines? `[RoutePrefix("prefix1")] [RoutePrefix("prefix2")]` – Zapnologica Oct 28 '15 at 13:22
  • 1
    @Zapnologica It's been a while since I did this but if I remember right you either do it like your example or you comma separate them like `[RoutePrefix("prefix1"), RoutePrefix("prefix2")]` try them both and see what happens – Matthew Verstraete Oct 28 '15 at 17:11
  • 3
    Dont thinks that's possible. It will throw Duplicate RoutePrefix attribute. – user1961100 Oct 29 '15 at 03:07
  • And I think you should put end sign `$` at the end of the each strings to disallow Gamesssdlasd, Cinemasasdasd etc. `[RoutePrefix("{Type:regex(Games$|Cinema$|Books$)}/{SectionRoute}/Character/")]` – Okan Kocyigit Feb 16 '18 at 07:06
  • https://github.com/asadmalik3/WebApiMultipleRoutePrefixAttribute This allows multiple routePrefix. – SatanEnglish Feb 27 '18 at 02:38