5

I really enjoyed the idea to use Attributes to control ASP.NET MVC routing, so instead of registering a new route and polute Global.asax.cs or *AreaRegistration.cs, I have something like this:

[Route("users/{userrId}/pages/{pageId}"]
public ActionResult Some(int userId, int pageId) { }

I see the great answer: ASP.NET MVC Routing Via Method Attributes, but I would like to get some "real-use" recommendations.

  1. Which routing framework you would recommend?
  2. How easy to start up and change existing code for usage?
  3. Is it available on NuGet?

.. and is it really worth to use that, can it be treated as best (perhaps good?) practice?

Community
  • 1
  • 1
Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86

3 Answers3

4

I did try routing via method attributes while ago. Wouldn't use any framework for routing.

I think it's best to just follow "standard" scheme.

Also - unit testing each route individually helped me quite a lot
(I even wrote test that fails in case I've missed any route).

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
2
  1. I would recommend AttributeRouting, I wrote it and use it every day.
  2. To start up, simply decorate an action with a GET attribute. Nuget does the rest.
  3. Nuget yes.

I like URLs.

I like seeing them right above the code they map to.

[GET("some/url")]
public ActionResult HotDamn()

When you do this, you can ctrl+F "some/url" and go right your action method, which is pretty handy. If you change your action name or move it somewhere else, the route stays with it and is unchanged.

[GET("some/url"]
public ActionResult HotDarnCauseCussingIsBad()

If you want to change URLs and still respond to the legacy URL, just add another attribute above the old one and mark it as the new one by setting Order = 1:

[GET("better/url", Order = 1)]
[GET("some/url")] // order defaults to int.MaxValue
public ActionResult BetterMethodName()

If you want to get restful, and go all the way with HTTP method overrides and support GET/POST/PUT/DELETE, you just go right on with your bad self:

[GET("")]
public ActionResult Index()

[POST("")]
public ActionResult Create()

[PUT("{id}")]
public ActionResult Update()

[DELETE("{id}")]
public ActionResult Delete()

I use this lib all the time, and made it so I could. I have been using it for a year and a half and I never ever have trouble with routing or finding where things go or wondering why route A got matched instead of route B. It's super easy to use, has quite a few configuration options, and makes routing 100x simpler (in my opinion).

Whatever you choose, I definitely recommend at least trying attribute based routing, whether AR or something else. May not be everybody's cup-o-tea, but I absolutely love it.

spot
  • 2,413
  • 2
  • 19
  • 16
0

My personal preference is to keep the routing configuration centralized (I keep it on the Global.asax).

However, whatever method you use, be sure you are following a standard. I guess the issue I see with attribute routing is that it allows for the developers to get "creative" in a way that it becomes confusing and difficult to maintain and understand.

Having all routing on a centralized place, lets you quickly find and understand the routing rules being applied.

gdoron
  • 147,333
  • 58
  • 291
  • 367
covo
  • 550
  • 4
  • 12