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

Categories

0 votes
1.3k 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 Reply

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...