I have a MVC4 project and I have an external project that sends emails out that refer to areas of the site.
Rather than hardcoding urls into the emails I want to be able to make sure that I get the canonical url from the routing. I can reference the MVC project which means I believe I should be able to get any compile time information that it has (which includes routes and things).
I have managed to create a RouteCollection
and fill it with my routes but I am now struggling with how I can query this collection. The only way I have found is the RouteUrl
method on UrlHelper
but this needs a UrlHelper
with the appropriate routing data which I am having trouble creating. I also would hope that there would be better methods available to query a routecollection.
Essentially I have this route:
routes.MapRoute
(
"ShowBlog",
"blog/{shortcode}/{slug}",
new { controller = "Blog", action = "ShowBlog", shortcode = "", slug = "" }
);
And I want some code like GetUrl("ShowBlog", new {shortcode = "foo", slug="bar"})
that will return blog/foo/bar
.
So how can I do this?