In some cases UrlHelper.Action returns null under .NET 4.0 after working in .NET 3.5http://forums.asp.net/t/1549043.aspx/1?In+some+cases+UrlHelper+Action+returns+null+under+NET+4+0+after+working+in+NET+3+5Tue, 04 Jan 2011 03:34:44 -050015490433793088http://forums.asp.net/p/1549043/3793088.aspx/1?In+some+cases+UrlHelper+Action+returns+null+under+NET+4+0+after+working+in+NET+3+5In some cases UrlHelper.Action returns null under .NET 4.0 after working in .NET 3.5 <p>Consider the following code snippet:</p> <p></p> <pre class="prettyprint">[AcceptVerbs(HttpVerbs.Post)] public ActionResult TagChange( string tag ) { ... } ... string action = Url.Action( &quot;TagChange&quot; );</pre><p><br> <br></p><p>Under .NET 3.5 the string action is "http://host/controller/TagChange" as expected but under 4.0 it's null.</p><p>Interestingly if I change the UrlHelper.Action call so that it matches the default routing i.e. <pre class="prettyprint">string Action = Url.Action( "TagChange", new { id = "foo" } );</pre> then the string action is the expected &quot;http://host/controller/TagChange/foo&quot; rather than null. <p></p> <p>I looked at the .NET 4.0 breaking changes and I can't for the life of me see anything that would cause this.</p> <p>Same version of MVC in both cases, everything identical aside from the targeted framework.</p> <p>Any ideas? I'd really like to move to 4.0.</p> <p>EDIT: I can't replicate with a minimum test case :( I'll keep plugging away but if anyone has any ideas please let me know<br> </p> 2010-04-20T01:11:42-04:004236296http://forums.asp.net/p/1549043/4236296.aspx/1?Re+In+some+cases+UrlHelper+Action+returns+null+under+NET+4+0+after+working+in+NET+3+5Re: In some cases UrlHelper.Action returns null under .NET 4.0 after working in .NET 3.5 <p>I use now .NET 4.0 MVC3 RC2</p> <p>I found the same problem as you. Also, I was surprised that it's hard to find simple solution for that in the internet.</p> <p>There is my solution:</p> <p>You can note that if you call @Url.Action with route values, e.g. @<span><span>Url.Action(&nbsp;</span><span>&quot;TagChange&quot;, new { id = 1 }</span><span> ); then it will work ok. But of course, we don't want to pass fake void route values. So, let's just add another map route to Global.asax, for example:</span></span></p> <p><br> </p> <p><span><span><pre class="prettyprint">routes.MapRoute(&quot;Simple&quot;, &quot;{controller}/{action}&quot;, new { controller = &quot;Home&quot;, action = &quot;Index&quot; }); routes.MapRoute(&quot;Default&quot;, &quot;{controller}/{action}/{id}&quot;, new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = UrlParameter.Optional });</pre><br> It's look like the issue that it requires route values even if they are declared as optional. So let's just add new map route (as shown above named &quot;Simple&quot;) without any parameters. Any you code should work just with usual @Url.Action(&quot;TagChange&quot;);<br> </span></span></p> 2011-01-03T20:57:38-05:004236569http://forums.asp.net/p/1549043/4236569.aspx/1?Re+In+some+cases+UrlHelper+Action+returns+null+under+NET+4+0+after+working+in+NET+3+5Re: In some cases UrlHelper.Action returns null under .NET 4.0 after working in .NET 3.5 <p>See,</p> <p><a href="http://weblogs.asp.net/imranbaloch/archive/2010/12/26/routing-issue-in-asp-net-mvc-3-rc-2.aspx">http://weblogs.asp.net/imranbaloch/archive/2010/12/26/routing-issue-in-asp-net-mvc-3-rc-2.aspx</a><br> </p> 2011-01-04T03:34:44-05:00