Subtleties with using Url.RouteUrl to get fully-qualified URLs

At some point I missed the Url.RouteUrl overload that took a protocol and returned an absolute URL based on the current context. It is quite handy when you are sending URLs out into the world (e.g., RSS feed link). I ended up using the less-handy overload that took an explicit host (same as the current, in this case) and passing it in. When someone pointed out the simpler overload, I did the obvious and deleted the host from the call. That didn’t quite work. For those looking for a way to get a fully-qualified URL through the route system, the less-than-obvious answer is to call the overload for Url.RouteUrl that gets a URL with a different protocol (Url.RouteUrl(string, object, string)), passing in Request.Url.Scheme for the current protocol. Url.RouteUrl(“Default”, new { action = “Index”, controller = “Department”, id = 1 }, Request.Url.Scheme); // Say you want to send someone to a different subdomain in your app while using the same routes. There’s an overload for that: Url.RouteUrl(string, RouteValueDictionary, string, string). Combined with the above example, here’s how these all play out if you are currently handling a request and your route table includes the fallback default ({controller}/{action}/{id}). Url.RouteUrl(“Default”, new… Continue reading

Handling case-insensitive enum action method parameters in ASP.NET MVC

Skip to solution Using enums as action method parameters Say you have a enum, a sorting enum in this case. public enum SortType { NewestFirst, OldestFirst, HighestRated, MostReviews } ASP.NET will gladly let you use that enum as an action method parameter; you can even make it an optional parameter. To make it optional by routing, you need to make it nullable for the action method function parameter in your controller and add some guarding logic (!sort.HasValue or the like). routes.MapRoute( “DepartmentProducts”, “Department/Products/{sort}”, new { controller = “Department”, action = “Products”, sort = UrlParameter.Optional } ); public ActionResult Products(SortType? sort) { SortType requestedSort = sort ?? SortType.NewestFirst; … } To make the parameter optional by function parameter, just give the parameter a default value. routes.MapRoute( “DepartmentProducts”, “Department/Products/{sort}”, new { controller = “Department”, action = “Products” } ); public ActionResult Products(SortType sort = SortType.NewestFirst) { … } Both work just fine, though I lean toward the function parameter default. Regardless of implementation, you can call the method by a number of different URLs: (sort == null or SortType.NewestFirst, respectively) Unfortunately, it requires the route value to be an exact match for the enum name, proper case included…. Continue reading