0

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?

Alexandre
  • 498
  • 2
  • 12
Chris
  • 27,210
  • 6
  • 71
  • 92
  • Assume the external project is a .NET project? Can you include the `System.Web.Routing` and `System.Web.Mvc` libraries in it? If so, I would think you should be able to fenagle a working `UrlHelper`. – danludwig Nov 06 '13 at 13:12
  • Just to confirm for the benefit of others since you've already given me an answer) that the external project is indeed also .NET. – Chris Nov 06 '13 at 13:25

1 Answers1

1

If you can wire up a UrlHelper from a test project, you can do it from any other external project. However you need to register the routes in the external project just like you do during Application_Start in the MVC project.

You will also need to mock up an HttpContextBase and a RequestContext, which means your external project will at least need to know the application path where your MVC project is installed. This would be easier if your external project could use a mocking library just like a test project would. Is that a possibility, or not?

If so, here is a link for how to do it with Moq. You don't need the controller, but you can use similar code for your HttpContext, RequestContext, and UrlHelper.

Community
  • 1
  • 1
danludwig
  • 46,965
  • 25
  • 159
  • 237
  • The external project is actually a web service so I'm a little nervous about registering the routes in it. Using a mocking library might make me better able to do things though since the creating the UrlHelper is a big sticking point currently. That Moq link might be really useful as well since we use Moq in our testing already. – Chris Nov 06 '13 at 13:24
  • 1
    That worked perfectly. I had to mock a couple more methods but the Mocking of the UrlHelper creation worked perfectly. Now I need to worry about whether it will drive me crazy having mock in production code. ;-) – Chris Nov 06 '13 at 15:10
  • Haha! Hey, whatever tool gets the job done quickly, easily, and cheaply, right? Just tell yourself that when it starts to drive you crazy. As far as routes, you can register your MVC routes in the web service safely. Just map them using a new RouteCollection instead of using RouteTable.Routes and it should be nicely encapsulated away. – danludwig Nov 06 '13 at 15:28
  • In the end it looks like this method went a bit too far off the beaten track in terms of using loads of other stuff so I'm just going for a slightly more hacky method of getting the url pattern and replacing with parameters and stuff like that. It might be a bit less robust but we shouldn't be changing routes often so it should all be good... I like this solution though. I did enjoy getting to it even if it looks like the code won't make it into live. – Chris Nov 06 '13 at 16:02