Routing Validation Based On HTTP Headers

Last post 01-08-2008 3:51 PM by ghotiman. 8 replies.

Sort Posts:

  • Routing Validation Based On HTTP Headers

    01-07-2008, 5:59 PM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 55

    In Scott Guthrie's post on Routing, he mentions that you can use the Validation property of a Route to specify an HTTP Header.

    "The ASP.NET MVC Framework allows you to use regular expressions to validate each parameter argument in the URL, as well as allows you to evaluate HTTP headers (to route URLs differently based on HTTP verbs)."

    However, I have not been able to get this to work.  I'm trying to filter on the host header.  Anyone have any ideas?  Thanks. 

    Filed under: , , ,
  • Re: Routing Validation Based On HTTP Headers

    01-07-2008, 8:38 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 690
    • ASPInsiders

    Ive not tested it with Host yet but it works with Method

    also be aware its case sensitive
     

  • Re: Routing Validation Based On HTTP Headers

    01-07-2008, 8:48 PM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 55

    I think the use of anonymous types here is part of the problem.  I have no idea what I can use for the Validation property.  I've seen examples of Method and controller/action/id used, but it seems as if there is just no way to know what can be used in this property.  Using:

    Validation = new { Host = "testhost:64701" }

     Does not work lower or upper case.  (testhost is added in hosts to point to 127.0.0.1)  Neither does HttpHost

  • Re: Routing Validation Based On HTTP Headers

    01-07-2008, 9:31 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 690
    • ASPInsiders

    Just done a bit of reflectoring in RouteCollection.ValidateRoute(). There is nothing in there to test Headers

    firstly, it has to be a string, i throws if it isn't. (so you can't pass in a Regex to overcome the case sensitivity.)

    secondly, it tests to see in the name of the parameter equals "method" and it tests this as a comma separated list of method names, NOT as a regular expression! so "GET,POST" not "GET|POST"

    thirdly it checks anything in the RouteData (which is anything matched in the Url and any Defaults specified.)

     
    Nothing to do with Headers yet.

     

     

  • Re: Routing Validation Based On HTTP Headers

    01-08-2008, 1:42 AM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 55

    Hmm... I see that too now.  That's too bad.  I wonder if it is possible to change the behavior of the RouteCollection...

    Right now I am getting the host name in the controller, and then rendering the appropriate view, but the ActionLinks are becoming a problem.

  • Re: Routing Validation Based On HTTP Headers

    01-08-2008, 2:17 AM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

     I believe routing will get a nice update.  There are some extensibility issues right now.  You can always create a custom route handler and your own extension method for ActionLinks.

    Shameless plug, but read my blog for some routing stuff and checkout mvccontrib source code for simply restful routing, it should give some examples of how to get what you need.

    Adam Tybor -- abombss.com
  • Re: Routing Validation Based On HTTP Headers

    01-08-2008, 11:02 AM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 55

    Hi Adam,

    I've actually already been checking out your blog, specifically your post on Simply RESTful Routing, which is great. (I even included a link above :)  I'm not sure if I can adapt it to my needs though.  You are setting the Method in the Validation property to route requests based on different HTTP Methods.  I want to route requests based on the host so I think I'd have to extend or replace the UrlRoutingModule, and I'm not sure that I can do that.

  • Re: Routing Validation Based On HTTP Headers

    01-08-2008, 12:23 PM
    Answer
    • Loading...
    • abombss
    • Joined on 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

    If you look at the destroy and update rules they use the SimplyRestfulRouteHandler, in this route handler is where I inspect the HttpRequest looking for form data, you can do something similar and look to the header.  Then just alter the route data putting in whatever controller, action, and other parameters you need and pass it back it of to ms mvc to handle.

    the only caveat is generating url's.  Since ms mvc doesn't understand your custom validation on the way in, it certainly will not understand it on the way out.  You can wrap regular actionlink calls with your own and apply your custom logic there to get the desired url.

    Adam Tybor -- abombss.com
  • Re: Routing Validation Based On HTTP Headers

    01-08-2008, 3:51 PM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 55

    Thanks abombs, I'll give that a try.

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter