<?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>Zero7Web &#187; Microsoft</title>
	<atom:link href="http://blog.zero7web.com/tag/microsoft/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.zero7web.com</link>
	<description>Web Design and Development</description>
	<lastBuildDate>Fri, 04 Jun 2010 17:48:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to: Allow hyphens in URL&#8217;s using ASP.NET MVC 2</title>
		<link>http://blog.zero7web.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/</link>
		<comments>http://blog.zero7web.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 14:28:03 +0000</pubDate>
		<dc:creator>Zero7Web</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Hyphens]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MVC 2.0]]></category>
		<category><![CDATA[URL Routing]]></category>

		<guid isPermaLink="false">http://blog.zero7web.com/?p=200</guid>
		<description><![CDATA[If you wan&#8217;t to allow hyphens in your URL&#8217;s you will need to change the way the routing works in your Global.asax file.
First create a new class which extends the MvcRouteHandler and place this in the Global.asax file after the main MvcApplication class.
C#:

public class HyphenatedRouteHandler : MvcRouteHandler&#123;
	protected override IHttpHandler  GetHttpHandler&#40;RequestContext requestContext&#41;
	&#123;
		requestContext.RouteData.Values&#91;&#34;controller&#34;&#93; = requestContext.RouteData.Values&#91;&#34;controller&#34;&#93;.ToString&#40;&#41;.Replace&#40;&#34;-&#34;, &#34;_&#34;&#41;;
		requestContext.RouteData.Values&#91;&#34;action&#34;&#93; [...]]]></description>
			<content:encoded><![CDATA[<p>If you wan&#8217;t to allow hyphens in your URL&#8217;s you will need to change the way the routing works in your Global.asax file.</p>
<p>First create a new class which extends the <strong>MvcRouteHandler</strong> and place this in the <strong>Global.asax</strong> file after the main <strong>MvcApplication</strong> class.</p>
<p>C#:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">public class HyphenatedRouteHandler <span style="color: #339933;">:</span> MvcRouteHandler<span style="color: #009900;">&#123;</span>
	protected override IHttpHandler  GetHttpHandler<span style="color: #009900;">&#40;</span>RequestContext requestContext<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		requestContext.<span style="color: #202020;">RouteData</span>.<span style="color: #202020;">Values</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">&quot;controller&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> requestContext.<span style="color: #202020;">RouteData</span>.<span style="color: #202020;">Values</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">&quot;controller&quot;</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">ToString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #202020;">Replace</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;-&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		requestContext.<span style="color: #202020;">RouteData</span>.<span style="color: #202020;">Values</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">&quot;action&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> requestContext.<span style="color: #202020;">RouteData</span>.<span style="color: #202020;">Values</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">&quot;action&quot;</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">ToString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #202020;">Replace</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;-&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> base.<span style="color: #202020;">GetHttpHandler</span><span style="color: #009900;">&#40;</span>requestContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>VB:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Public</span> Class HyphenatedRouteHandler
    Inherits MvcRouteHandler
&nbsp;
    Protected Overrides <span style="color: #000080;">Function</span> GetHttpHandler(<span style="color: #000080;">ByVal</span> requestContext <span style="color: #000080;">As</span> System.Web.Routing.RequestContext) <span style="color: #000080;">As</span> System.Web.IHttpHandler
        requestContext.RouteData.Values(&quot;controller&quot;) = requestContext.RouteData.Values(&quot;controller&quot;).ToString.Replace(&quot;-&quot;, &quot;_&quot;)
        requestContext.RouteData.Values(&quot;action&quot;) = requestContext.RouteData.Values(&quot;action&quot;).ToString.Replace(&quot;-&quot;, &quot;_&quot;)
        Return MyBase.GetHttpHandler(requestContext)
    <span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">End</span> Class</pre></div></div>

<p>Then you need to replace the <strong>routes.MapRoute</strong> with an equivalent <strong>routes.Add</strong> specifying the new handler as the MapRoute does not allow you to specify a custom route handler.</p>
<p>C#:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">routes.<span style="color: #202020;">Add</span><span style="color: #009900;">&#40;</span>
	new Route<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;{controller}/{action}/{id}&quot;</span><span style="color: #339933;">,</span> 
		new RouteValueDictionary<span style="color: #009900;">&#40;</span>
			new <span style="color: #009900;">&#123;</span> controller <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Default&quot;</span><span style="color: #339933;">,</span> action <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Index&quot;</span><span style="color: #339933;">,</span> id <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			new HyphenatedRouteHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>VB:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">routes.Add(<span style="color: #000080;">New</span> Route(&quot;{controller}/{action}/{id}&quot;, 
	<span style="color: #000080;">New</span> RouteValueDictionary(<span style="color: #000080;">New</span> <span style="color: #000080;">With</span> {.controller = &quot;Home&quot;, .action = &quot;Index&quot;, .id = &quot;&quot;}), 
		<span style="color: #000080;">New</span> HyphenatedRouteHandler()))</pre></div></div>

<p>I hope this is useful, any questions feel free to get in touch.</p>
<p>Would like to say thanks to John from <a href="http://stackoverflow.com/questions/2070890/asp-net-mvc-support-for-urls-with-hyphens">here</a> for answering the original question on <a href="http://stackoverflow.com">Stackoverflow</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zero7web.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
