开发者

LogOnPartial renders with a blank href - MVC 3

I'm using a standard MVC 3 Web Project and I've written my HTML out in a standard HTML file, and then copy/pasted it into the _layout.cshtml file. With all the correct RenderBody() and @Html.Partial("_LogOnPartial") included the page works fine, but the ActionLink inside the _LogOnPartial doesn't render an href.

Html.ActionLink("Log On", "LogOn", "Account")

Will come out as开发者_开发百科:

<a href="">Log On</a>

This is unchanged from the standard link that you get when you start an MVC 3 Web project.

The registered Routes are:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Products",
            "{controller}/{action}/{id}",
            new {controller = "Products", action = "Index"});

        routes.MapRoute(
        "Newsletter",
        "{controller}/{action}/{emailAddress}",
        new { controller = "Newsletter", action = "Index" });

        routes.MapRoute(
       "Account",
       "{controller}/{action}/{id}",
       new { controller = "Account", action = "LogOn", id = UrlParameter.Optional });

    }

I don't understand why this is happening, but in Visual Studio it is not underlining the Action or Controller which makes me think it isn't seeing the AccountController properly. Anyone got any ideas?


Fabian is right. The problem is because there are too many routes, many of which are almost identical in their pattern. Your "Default", "Products" and "Account" routes all look for a controller, an action and an optional ID (either explicitly or implicitly). You would probably have considerably more success if you just set the default controller back to its factory-standard form like so:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }); // Parameter defaults


        routes.MapRoute(
        "Newsletter",
        "{controller}/{action}/{emailAddress}",
        new { controller = "Newsletter", action = "Index" });

    }

Since the ID is optional, your Products routes will still be matched if you supply an ActionLink with no ID (e.g. @Html.ActionLink("My Link", "Products", "Index")) and the form of the "Account" route is identical to the "Default" one anyway. There's no need to use it and MVC is likely being tripped up between these, which is causing your LogOn partial's Href to fail.


ActionLink doesn't look at the controller or it's actions, just the routing table. Make sure you have a route with action "LogOn" and controller "Account" set in your global.asax.

Edit:
I recommend you read up a bit on mvc routing. Your current routes are too greedy and will match when they shouldn't.

For LogOn i would use something like this (put it at the top because its the least greedy since it doesnt contain any variables):

routes.MapRoute(
       "Account",
       "logon",
       new { controller = "Account", action = "LogOn", id = UrlParameter.Optional });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜