<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Captain Codeman&#187; Routing</title>
	<atom:link href="http://www.captaincodeman.com/category/mvc/mvc-routing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.captaincodeman.com</link>
	<description>Software Developer</description>
	<lastBuildDate>Fri, 15 Jul 2011 22:50:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Absolute URLs using MVC (without extension methods)</title>
		<link>http://www.captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/</link>
		<comments>http://www.captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 17:29:49 +0000</pubDate>
		<dc:creator>Captain Codeman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Rendering]]></category>
		<category><![CDATA[Routing]]></category>
		<category><![CDATA[action]]></category>

		<guid isPermaLink="false">http://blogs.intesoft.net/post.aspx?id=019a9ef7-023a-46c7-97a9-94fd14f7a28e</guid>
		<description><![CDATA[Do you need to generate absolute URLs within your MVC application? Often this will be in the form of URLs used outside of the web-browser such as those used within Atom Publishing Protocol collections or maybe links that are going to be sent out in emails. Basically, anything where the regular relative URL won’t do.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Do you need to generate absolute URLs within your MVC application?</p>
<p>Often this will be in the form of URLs used outside of the web-browser such as those used within Atom Publishing Protocol collections or maybe links that are going to be sent out in emails. Basically, anything where the regular relative URL won’t do.</p>
<p>A quick search of Google will turn up a number of blog posts or forum answers showing how to do this by creating extension methods for the Url helper class but really, everything that is needed is already baked into the MVC framework already … and I’ve only just realized it after using it since the CTP releases!</p>
<p><span id="more-4"></span></p>
<p>All you need to do is specify the protocol within the Url.Action or Url.RouteUrl method. Here are some regular looking URLs:</p>
<pre class="csharpcode">&lt;%= Url.Action(<span class="str">"About"</span>, <span class="str">"Home"</span>) %&gt;&lt;br /&gt;
&lt;%= Url.RouteUrl(<span class="str">"Default"</span>, <span class="kwrd">new</span> { Action = <span class="str">"About"</span> }) %&gt;&lt;br /&gt;</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>Which produce the same output:</p>
<p>/Home/About</p>
<p>/Home/About</p>
<p>If we add the protocol then it changes to:</p>
<pre class="csharpcode">&lt;%= Url.Action(<span class="str">"About"</span>, <span class="str">"Home"</span>, <span class="kwrd">null</span>, <span class="str">"http"</span>) %&gt;&lt;br /&gt;
&lt;%= Url.RouteUrl(<span class="str">"Default"</span>, <span class="kwrd">new</span> { Action = <span class="str">"About"</span> }, <span class="str">"http"</span>) %&gt;&lt;br /&gt;</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>http://localhost:51868/Home/About</p>
<p>http://localhost:51868/Home/About</p>
<p>Instead of having the ‘http’ hard-coded like that you can instead use whatever protocol was used for the request so URLs will be correct whether they are using http or https (assuming they don’t need to be a specific one), e.g.:</p>
<pre class="csharpcode">&lt;%= Url.Action(<span class="str">"About"</span>, <span class="str">"Home"</span>, <span class="kwrd">null</span>, Request.Url.Scheme) %&gt;&lt;br /&gt;
&lt;%= Url.RouteUrl(<span class="str">"Default"</span>, <span class="kwrd">new</span> { Action = <span class="str">"About"</span> }, Request.Url.Scheme) %&gt;&lt;br /&gt;</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>If you just pass the protocol / scheme then the host name and port number are generated automatically based on the site that the app is running on. You can also pass the host name as well if you want (if the public host name doesn’t match the one the site is actually running on).</p>
<p>This works for the Url helper only, the protocol isn’t an option in the Html helper used to general complete html anchor links but I’m guessing that if you need an absolute URL you are needing something else beyond a plain link anyway and it isn’t too much trouble to just define the anchor element in a view and use the Url helper in the href attribute only.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

