Archives - Tag: ASP.NET MVC

  • 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).

  • 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.

  • Initial setup for a new ASP.NET MVC site in IIS7

    Background

    Over the years, I have spent far too many hours running the same set of commands against ASP.NET and ASP.NET MVC sites to bring them up to what I consider a starting point. Most of the time, I have to refresh myself about how to do at least one of them. This is a list of those commands in a central location for myself and anyone else to use. As with any good instructions, there is no guarantee you won’t completely destroy your server, site, or soul using these commands. I can only say they worked for me once.

  • Where did that JSON field go? Serializing IHtmlString to JSON

    TL;DR

    If your brain consumes Stack Overflow questions better than blog posts, go see “How do I serialize IHtmlString to JSON with Json.NET?” over there.