@ Html.ActionLink返回一个参数化的超级链接 [英] @Html.ActionLink returning a parameterised hyperlink

查看:159
本文介绍了@ Html.ActionLink返回一个参数化的超级链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话:

  @ Html.ActionLink(注册,注册,帐户,新{面积=前端})

希望

  www.example.com/Account/Register

但我发现

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

我,为什么出现这种情况有点困惑,我能做些什么来解决这个问题?

修改

当我删除区域参数末,它建立链接正确,但我的管理部分,而不是前端

修改

我就隔离到一个自定义路由我已经安装:

  routes.Add(CategoriesRoute,新CategoriesRoute()); //这是打破ActionLink的。routes.MapRoute(默认,
    {控制器} / {行动} / {ID}
    新{控制器=家,行动=索引,面积=前端,ID = UrlParameter.Optional},
    新的[] {CC.Web.Areas.FrontEnd.Controllers}
    );

这是自定义路线。

 公共类CategoriesRoute:路线
{
    公共CategoriesRoute()
        :基地({*}类别,新MvcRouteHandler())
    {
    }    公众覆盖的RouteData GetRouteData(HttpContextBase的HttpContext)
    {
        VAR RD = base.GetRouteData(HttpContext的);
        如果(RD == NULL)
            返回null;        串类= rd.Values​​ [类别]作为字符串;
        如果(categories.IsEmpty())
            返回null;        字符串[] =零件categories.Split('/');
        如果(不是类)//伪code
            返回null;        rd.Values​​ [控制器] =类别;
        rd.Values​​ [行动] =指数;
        rd.Values​​ [区] =前端;
        rd.DataTokens.Add(命名空间,新的String [] {CC.Web.Areas.FrontEnd.Controllers});
        rd.Values​​ [categoryString] =/+类别; //需要得到上的缓存精确匹配。        返回RD;
    }
}


解决方案

尝试以下。我想你错过了一个属性

  @ Html.ActionLink(注册,注册,帐户,新{面积=前端},NULL)

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

解决方案

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

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

这篇关于@ Html.ActionLink返回一个参数化的超级链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆