Full URLs in MVC Action Links

Using a Url.Action("action", "controller", new { id = 123 }) will generate a URL (by default, at least) like so: "/controller/action/123".

This suits most cases just fine. However, there are certain cases where you might want the fully qualified URL. To do this, simply use the overload asking for protocol. You don’t have to provide the hostname:

Url.Action("action", "controller", new { id = 123 }, Request.Url.Scheme) uses the current request’s scheme (http/https) to produce "http://average-joe.info/controller/action/123".

Obviously there are variations on the above depending on exactly what you want. Here are some further examples:

Url.Action("action", "controller", null, Request.Url.Scheme)"http://average-joe.info/controller/action"
Url.Action("action", "controller", null, "http")"http://average-joe.info/controller/action"

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s