@Html.ActionLink 返回一个参数化的超链接 [英] @Html.ActionLink returning a parameterised hyperlink
本文介绍了@Html.ActionLink 返回一个参数化的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我打电话:
@Html.ActionLink("Register", "Register", "Account", new {area="FrontEnd"})
期待
www.example.com/Account/Register
但我得到了
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?
编辑
当我最后删除 area 参数时,它会正确构建链接,但链接到我的管理部分而不是 FrontEnd
.
When I remove the area parameter at the end, it builds the link correctly but to my admin section instead of FrontEnd
.
编辑
我已将其与已设置的自定义路由隔离:
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" }
);
这是自定义路由.
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;
}
}
推荐答案
试试下面的方法.我认为你最后缺少一个属性 null
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屋!
查看全文