@ Html.ActionLink返回参数化超链接

时间:2014-10-29 23:47:46

标签: asp.net-mvc asp.net-mvc-5

我在打电话:

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

期待

www.example.com/Account/Register 

但我正在

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

对于为什么会发生这种情况,我有点困惑,我该怎么做才能解决这个问题?

修改

当我删除末尾的区域参数时,它会正确构建链接,但是我的管理部分而不是FrontEnd

修改

我已将其隔离到我设置的自定义路线:

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" }
    );

这是自定义路线。

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;
    }
}

2 个答案:

答案 0 :(得分:2)

尝试以下方法。我认为你最后错过了一个属性null

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

答案 1 :(得分:1)

您需要为第5个参数添加null(html属性)

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