I found the same problem as you. Also, I was surprised that it's hard to find simple solution for that in the internet.
There is my solution:
You can note that if you call @Url.Action with route values, e.g. @Url.Action( "TagChange", new { id = 1 } ); 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:
routes.MapRoute("Simple",
"{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
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 "Simple") without any parameters. Any you code should work just with usual @Url.Action("TagChange");
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Nik Coughlin
Member
34 Points
16 Posts
In some cases UrlHelper.Action returns null under .NET 4.0 after working in .NET 3.5
Apr 20, 2010 01:11 AM|LINK
Consider the following code snippet:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult TagChange( string tag ) { ... } ... string action = Url.Action( "TagChange" );Under .NET 3.5 the string action is "http://host/controller/TagChange" as expected but under 4.0 it's null.
Interestingly if I change the UrlHelper.Action call so that it matches the default routing i.e.
string Action = Url.Action( "TagChange", new { id = "foo" } );then the string action is the expected "http://host/controller/TagChange/foo" rather than null.I looked at the .NET 4.0 breaking changes and I can't for the life of me see anything that would cause this.
Same version of MVC in both cases, everything identical aside from the targeted framework.
Any ideas? I'd really like to move to 4.0.
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
MaxSunlight
Member
2 Points
1 Post
Re: In some cases UrlHelper.Action returns null under .NET 4.0 after working in .NET 3.5
Jan 03, 2011 08:57 PM|LINK
I use now .NET 4.0 MVC3 RC2
I found the same problem as you. Also, I was surprised that it's hard to find simple solution for that in the internet.
There is my solution:
You can note that if you call @Url.Action with route values, e.g. @Url.Action( "TagChange", new { id = 1 } ); 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:
routes.MapRoute("Simple", "{controller}/{action}", new { controller = "Home", action = "Index" }); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });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 "Simple") without any parameters. Any you code should work just with usual @Url.Action("TagChange");
action Route conflicts null url address
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: In some cases UrlHelper.Action returns null under .NET 4.0 after working in .NET 3.5
Jan 04, 2011 03:34 AM|LINK
See,
http://weblogs.asp.net/imranbaloch/archive/2010/12/26/routing-issue-in-asp-net-mvc-3-rc-2.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD