Answered by:
Route Parsing (Uri to Route)

Question
-
User565651763 posted
I have a system that will be interacting with a website that leverages the MVC framework. Here's the scenario:
- I have a Uri object
- I have all the route information from the separate MVC site
- I'd like to be able to basically ask "give me the route that you would use given this Uri"
- With the result, I'd like to parse out the values of the tokens.For example - I pass in "http://www.mysite.com/controller/w00t/" and get returned the route that has the handler for "controller/{name}" and, ideally, the value of {name} in a collection of the tokens (in this case, "w00t").
Hopefully this makes sense and I'm wondering if it's possible at the moment to do this?
Cheers,
John-Daniel Trask
Friday, June 27, 2008 1:02 AM
Answers
-
User104195919 posted
Hi John-Daniel,
The method you are looking for is RouteTable.Routes.GetRouteData, but unfortunately it only takes an HttpContextBase. Creating a fake version of this class to hold your Uri gets complicated as constraints applied to a route could, in theory, access anything available in the HttpContextBase object graph.
If you do want to go down the path of this, you will definitely need to expose HttpContextBase.Request, HttpRequestBase.AppRelativeCurrentExecutionFilePath, and HttpRequestBase.PathInfo. Perhaps the best course of action would be to create an HttpContextBase wrapper that exposes the properties of an existing HttpContextBase, and an HttpRequestBase wrapper that exposes the properties of an existing HttpRequestBase but returns a different value for Uri.
In any case, its alot of work.
Sorry I couldn't be more help.
Richard
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 27, 2008 3:24 AM
All replies
-
User104195919 posted
Hi John-Daniel,
The method you are looking for is RouteTable.Routes.GetRouteData, but unfortunately it only takes an HttpContextBase. Creating a fake version of this class to hold your Uri gets complicated as constraints applied to a route could, in theory, access anything available in the HttpContextBase object graph.
If you do want to go down the path of this, you will definitely need to expose HttpContextBase.Request, HttpRequestBase.AppRelativeCurrentExecutionFilePath, and HttpRequestBase.PathInfo. Perhaps the best course of action would be to create an HttpContextBase wrapper that exposes the properties of an existing HttpContextBase, and an HttpRequestBase wrapper that exposes the properties of an existing HttpRequestBase but returns a different value for Uri.
In any case, its alot of work.
Sorry I couldn't be more help.
Richard
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 27, 2008 3:24 AM -
User-1902356349 posted
Why not use a mocking framework to create a mock of HttpContextBase. Then just pass that to GetRouteData.
See Robs video on mocking for more info.
http://blog.wekeroad.com/mvc-storefront/mvcstore-part-12/
Friday, June 27, 2008 9:58 AM -
User104195919 posted
tgmdbm's idea is great as long as the code is intended for unit tests. Creating mock objects has quite a large overhead and I would recommend it for production code.Friday, June 27, 2008 11:31 AM -
User-1902356349 posted
Ok... you could create your own mocks...
public class MyMockHttpContext : HttpContextBase {
// override the necessary properties and methods here ....
}
... you wouldn't need to override every property and method, just the ones which GetRouteData use. Effectively, the same methods and properties which are mocked in MvcMockExtensions.
This would have no significant overhead at all!
Friday, June 27, 2008 11:40 AM -
User565651763 posted
Thanks for the guidence folks, I've now got this working without too much hassle (did just create a fake context with various fake dependencies).
Cheers,
John-Daniel Trask
Friday, June 27, 2008 11:51 PM -
User2030557448 posted
Hello,
I am interested in this solution - can you post the code for your fake context
thanks
Monday, September 15, 2008 1:16 PM -
User788254762 posted
You could create a service on your MVC site that takes the URI as input and returns the desired tokens as its return value. That way, you don't need to maintain two routing tables or mock anything up.Monday, September 15, 2008 1:42 PM -
User2030557448 posted
Hi,
We arent using MVC just the routing engine and we need to use that really to return the correct route based on the URI , otherwise its just our interpretation of how the routing works and how the routing actually works
Thanks
Tuesday, September 16, 2008 4:38 AM