开发者

MVC Routes - How to get a URL?

In my current project we have a notification system. When an oject is added to another objects collection, an email is sent to those who are subscibed to the parent object. This happens on the object layer and not in the View or Controller.

Here's the problem:

Although we can say who created what with what information in the email, we cannot embed links to those开发者_如何学Python objects in the email because in the object layer there is no access to a UrlHelper. To construct a UrlHelper you need a RequestContext, which again does not exist on the object layer.

Question:

I want to make a helper class to create the url's for me. How can I create an object that will generate these urls without a request context? Is it possible?


The problem is compounded by the fact that you don't want a relative URL in an email, you want an absolute email so you need to hard-code the domain too because there is no request to grab it from.

Another factor is that emails can outlive the current site structure by months or years so you need a kind of permalink, and thus a way to associate multiple Urls with a single action (additional routes). This latter issue is also a factor in SEO where you don't want to leave any page behind.

For now a static method on your controller UrlToActionX(params) sitting next to the method ActionX seems like the simplest workaround. All it does is the appropriate string.Format(...) on the id's of the strongly-typed parameters to generate the permanent Url. Add a static domain on the front, or a domain from the user object (since you know which domain they visit when they come to your site) and you have your email link.

It's not ideal but at least you now have only one place to maintain the Url generation.

IMHO: When it comes to permanent links to a changing web site sometimes it's better to rely on "configuration over convention". :-)


I'm not aware of a way to do this, you MUST have access to the routes at the very least to make your own helper. Unless your business objects know about the registered routes, you can't get away from doing some hard-coding.

Here is how you might limit the hard-coding of urls though...

Code in a url with all the relevant bits in your object's methods..

    class Event
    {
        public void SendEmail()
        {
            var url = string.Format("http://myurl.com/r/Event?eventId={0}", EventId);
            //send emails...
        }
    }

Note the /r/Event piece of the url. This would be a map to a RController that would be responsible for taking arbitrary, made-up links and sending a 301 Permanent Redirect and going through the route engine to create a real url using the current routes. This way you are only hard-coding a utility controller url and not to the ever evolving controller actions of your real pages.

class RController : Controller
{
    public ActionResult Event(int eventId)
    {
        Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
        Response.RedirectLocation = Url.Action("Details", "Event", new { eventId = eventId });
        return null;
    }

    public ActionResult Register(int eventId)
    {
        Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
        Response.RedirectLocation = Url.Action("Register", "Event", new { eventId = eventId });
        return null;
    }
}

It just feels a bit better than hard-coding a bunch of different controllers/actions that you might decide to rename later. Think of it as your own little TinyUrl like service.


You could define an interface with a method that takes whatever information is necessary to create a URL (object ids or whatever) and returns a URL. Write an implementation of that interface that uses the UrlHelper to do this work, and then supply this to your object layer (ideally with an IoC container).


You could use:

VirtualPathUtility.ToAbsolute(string.Format("~/r/Event?eventId={0}", id))

to resolve the url. Still not nice though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜