This project is read-only.

Url.Action is returning null

Mar 11, 2013 at 10:49 PM
What is required to get the various extension methods (URL.Action, Html.ActionLink, etc..) to return a proper URL? Seems that no matter what I pass to these methods, it won't match any routes. I don't get any exception, I just get an null or empty href (depending on the exact method used).

Here's a brief description of the issue in my environment (cut down to show just 1 controller).

I've got the following controller defined...
with two actions...
/Routes.axd shows...
routes.MapRoute(null, "Authentication/Account/{action}", 
    new { controller = @"Account" }, 
    new { action = @"LogOn|LogOff" }, 
    new[] { "MyCompany.WebApp.Controllers.Authentication" });
I can manually navigate to /Authentication/Account/LogOn & /Authentication/Account/LogOff and both actions return the expected view.

However, if I put something like the code below into my Home/Index.cshtml page, every single one of the Url.Action calls returns null and the Html.ActionLink calls render an A tag with an empty href attribute.
    <li>@Url.Action("LogOut", "~~Authentication.Account")</li>
    <li>@Url.Action("LogOut", "~Authentication.Account")</li>
    <li>@Url.Action("LogOut", "Authentication.Account")</li>
    <li>@Url.Action("LogOut", "Account")</li>

    <li>@Html.ActionLink("Test LogIn", "LogIn", "~~Authentication.Account")</li>
    <li>@Html.ActionLink("Test LogIn", "LogIn", "~Authentication.Account")</li>
    <li>@Html.ActionLink("Test LogIn", "LogIn", "Authentication.Account")</li>
    <li>@Html.ActionLink("Test LogIn", "LogIn", "Account")</li>
Mar 11, 2013 at 11:07 PM
This could happen if the current route was not created by MvcCodeRouting, in this case the HomeController. Is that the case here?
Mar 12, 2013 at 3:12 AM
No, all routes are created using the default MvcCodeRouting options. I did not manually create any routes, and the index action on the home controller does show up in the routes.axd output.
Mar 12, 2013 at 3:43 AM
I just noticed, the routes show LogOn|LogOff, but the view uses LogIn/LogOut :-)
Marked as answer by maxtoroq on 10/15/2013 at 9:20 AM
Mar 12, 2013 at 3:30 PM
I hate it when that happens. :)
After getting a good night of sleep, I noticed the same thing.