1

I have the following configured route:

routes.MapHttpRoute("oneKey",
                    "{controller}/{id}");

If I go to the endpoint /users/Maticicero, the route will be correctly delegated to the GET method in my UsersController class.

However I am getting the id parameter just as it was typed, like Maticicero. Since this is the primary key of my database, and the comparision is case sensitive, I need to normalize my ids to lowercase, that is:

id = id.ToLower()

I have to do this in every controller and in every method that takes the id parameter.

Is there a way to tell my Web Api (or route handler) to automatically lower case these parameters (or the whole url)?

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • 2
    @naveen That's a Route Constraint. It won't modify the value, it will just fail to match the route if it's not lower case. This isn't what the OP asked for. – spender Jul 03 '15 at 02:09
  • yep. still sleepy i guess. deleting comment :) – naveen Jul 03 '15 at 02:14
  • Why not use a Primary Key that can't be confused in lookups. E.g an INT or GUID. And if you ever need to search on name use `Name LIKE '%name%'` – Xenolightning Jul 03 '15 at 03:02
  • @Xenolightning We're currently using AWS `DynamoDB` as our persistence strategy. While having an int key was feasible, the server would be responsible for generating the associated ids (because DynamoDB does not support auto incremental keys), this would make us create a GUID for each user, those messing up the URL. We chose the username as the primary key instead for this reason (it also complies with the requirement that the username must be unique) – Matias Cicero Jul 03 '15 at 11:26
  • @MatiCicero Are you querying the DynamoDb directly from you controllers? If so, you may want to look at using repositories over your data access. It would allow you to implement consistent logic for your Db queries. `Controller -> UserRepository (call ToLower here) -> DynamoDb`. Relying solely on WebApi to give you the correct case of a string is very fragile code. – Xenolightning Jul 05 '15 at 20:49

2 Answers2

1

Try to use routes.LowercaseUrls = true; when you register your routes. MSDN

RouteCollection.LowercaseUrls: Gets or sets a value that indicates whether URLs are converted to lower case when virtual paths are normalized.

But be careful when you use your requests like this http://host/controller?id=Maticicero

If a query string is included in the URL, that part of the URL is not converted to lower case.

Also take a look at this SO question: How can I have lowercase routes in ASP.NET MVC?

Community
  • 1
  • 1
Maksim Vi.
  • 9,107
  • 12
  • 59
  • 85
  • 2
    Unfortunately, I'm using ASP.NET Web Api and not MVC, so I am given a `HttpRouteCollections` instead of the `RouteCollections` you mention. That class does not have this property :( – Matias Cicero Jul 03 '15 at 11:17
0

You can trap the URL in the Application_BeginRequest event, parse the URL, and use HttpContext.RewritePath to modify the original URL.

https://msdn.microsoft.com/en-us/library/sa5wkk6d(v=vs.110).aspx

Jaime
  • 5,770
  • 4
  • 23
  • 50