views:

119

answers:

4

How would I generate a proper URL for an MVC application to be included in an e-mail?

This is for my registration system which is separate from my controller/action. Basically, I want to send an email verification to fire an Action on a Controller. I don't want to hardcode the URL in, I would want something like the Url property on the Views.

+2  A: 

In your Controller, the UrlHelper is just called "Url" - so:

void Index() {
    string s = this.Url.Action("Index", "Controller");
}

The "this" is unnecessary, but it tells you where this Url variable comes from

Paul Betts
This won't work for me since I am using my Provider/Service model. I need something in my Provider/Service not my controller.
Daniel A. White
A: 

If I'm reading the question correctly, you need to controller/action outside the MVC code. If so, you will need to simply configure the URL in Application Configuration or some such place, unless you have access to the controller classes and use reflection to get the names.

Greg Ogle
A: 

You should probably make the URL part of the configuration of your application.

I know you can do stuff with e.g. the Server property on your web application, but the application will never know if its IP or domain name is reachable from the outside as it might be hidden behind a proxy or a load balancer.

mookid8000
+1  A: 

I used:

Html.BuildUrlFromExpression<AccountController>(c=>c.Confirm(Model.confirmedGUID.Value))

It is part of the HTMLHelper (I think in the MVC Futures) so you may have to pass an instance of the HTMLHelper to your service layer, not sure. I use this directly in my view which renders to an email. That gives you the absolute URL and then I store the domain (http://www.mysite.com) in the config file and append it before the URL.

Jonathan