tags:

views:

124

answers:

2

I have a class library that sends out emails for various events in my application. part of the email is a link to get back to the affected area of the site.

Is there the equivalent to Url.Action() for a class library to generate the link?

A: 

Assuming that the email is generated by somewhere in your MVC site, you could always pass through the Url from the controller into your class library in order to create the url there...

Your controller

public ActionResult SendOutEmail()
{
  MyClass myClass = new MyClass();
  CreateMyEmail(Url, myClass);

  return View();
}

Your library class here...

public static void CreateMyEmail(UrlHelper url, MyClass informationToSend)
{
  string myUrl = url.Action(...);

  //and the rest of your class...
}
Dan Atkinson
I don't know if this is the correct way of doing it but it works
William
It's not the 'wrong' way to do it, but then, if sending an email is a result of something that's happened on your site, then yes, it's perfectly fine, as you're just moving the flow out into a class library, but passing in something it needs for it to work.
Dan Atkinson
A: 
var requestContext = ControllerContext.RequestContext;
var routeData = ControllerContext.RouteData;

...

var path = routeData.Route.GetVirtualPath(requestContext, routeData.Values).VirtualPath;
takepara