Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

asp.net mvc - @Html.ActionLink returning a parameterised hyperlink

I am calling:

@Html.ActionLink("Register", "Register", "Account", new {area="FrontEnd"})

expecting

www.example.com/Account/Register 

but I'm getting

http://www.example.com/?action=Register&controller=Account&area=FrontEnd

I'm a little confused as to why this happening, what can I do to resolve this?

Edit

When I remove the area parameter at the end, it builds the link correctly but to my admin section instead of FrontEnd.

Edit

I've isolated it to a custom route I have setup:

routes.Add("CategoriesRoute", new CategoriesRoute()); //This is breaking ActionLink.

routes.MapRoute("Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", area = "FrontEnd", id = UrlParameter.Optional },
    new[] { "CC.Web.Areas.FrontEnd.Controllers" }
    );

This is the custom route.

public class CategoriesRoute : Route
{
    public CategoriesRoute()
        : base("{*categories}", new MvcRouteHandler())
    {
    }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var rd = base.GetRouteData(httpContext);
        if (rd == null)
            return null;

        string categories = rd.Values["categories"] as string;
        if (categories.IsEmpty())
            return null;

        string[] parts = categories.Split('/');
        if (not a category) //pseudo code
            return null;

        rd.Values["controller"] = "Category";
        rd.Values["action"] = "Index";
        rd.Values["area"] = "FrontEnd";
        rd.DataTokens.Add("namespaces", new string[] { "CC.Web.Areas.FrontEnd.Controllers" });
        rd.Values["categoryString"] = "/" + categories; //Required to get exact match on the cache.

        return rd;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try the below. I think you're missing an attribute null at the end

@Html.ActionLink("Register", "Register", "Account", new {area="FrontEnd"}, null)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...