Asp.net mvc 4 不带参数的MVC4 ActionLink指向当前路径
我有一个MVC4索引页面,其中包含以下链接:Asp.net mvc 4 不带参数的MVC4 ActionLink指向当前路径,asp.net-mvc-4,iis,actionlink,html.actionlink,Asp.net Mvc 4,Iis,Actionlink,Html.actionlink,我有一个MVC4索引页面,其中包含以下链接: @Html.ActionLink("FOO", "Index", "Mil", new { year = String.Empty, id = String.Empty }, null) @Html.ActionLink("BAR", "Index", "Euro", new { year = String.Empty, id = String.Empty }, null) 但奇怪的是,两者都变成了指向当前路径的超链接(即,如果我们位于域/Euro
@Html.ActionLink("FOO", "Index", "Mil", new { year = String.Empty, id = String.Empty }, null)
@Html.ActionLink("BAR", "Index", "Euro", new { year = String.Empty, id = String.Empty }, null)
但奇怪的是,两者都变成了指向当前路径的超链接(即,如果我们位于域/Euro/2016/1则都指向此地址),而不是指向指定的控制器
线路图中的一切正常:
routes.MapRoute(
name: "Default",
url: "{controller}/{year}/{id}",
defaults: new { controller = "Home", action = "Index", year = UrlParameter.Optional, id = UrlParameter.Optional }
);
public ActionResult Index(int? year, int? id)
{
...
}
控制器/动作存在且工作正常:
EuroController:
routes.MapRoute(
name: "Default",
url: "{controller}/{year}/{id}",
defaults: new { controller = "Home", action = "Index", year = UrlParameter.Optional, id = UrlParameter.Optional }
);
public ActionResult Index(int? year, int? id)
{
...
}
是什么导致了这种奇怪的行为
编辑:
如果我通过了year参数,工作正常:
@Html.ActionLink(“FOO”,“Index”,“Mil”,new{year=666,id=String.Empty},null)
正确地指向域/Mil/666我能够通过在默认路径之后添加这个新的路径使它工作
routes.MapRoute(
name: "Default",
url: "{controller}/{year}/{id}",
defaults: new { controller = "Home", action = "Index", year = "", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "New",
url: "{controller}/{year}",
defaults: new { controller = "Home", action = "Index", year = UrlParameter.Optional }
);
以下所有链接都会产生所需的效果:
@Html.ActionLink("Euro 2014", "Index", "Euro", new { year = 2014 }, null)
@Html.ActionLink("Euro 2015", "Index", "Euro", new { year = 2015, id = String.Empty }, null)
@Html.ActionLink("Euro 2016", "Index", "Euro", new { year = DateTime.Now.Year, id = "" }, null)
@Html.ActionLink("Back to Euro", "Index", "Euro", new { year = String.Empty }, null)
可能不是问题,但只有最后一个参数可以标记为
urlparmeter。可选的I编辑了我的问题。这不是解决方案,但肯定是相关的。我更改了year=UrlParameter。按照您的建议,在路由到year=”“
时可选,但问题仍然存在。请使用null
ie:@Html.ActionLink(“FOO”,“Index”,“Mil”,new{year=null,id=null},null)
或将它们一起删除,即:@Html.ActionLink(“FOO”,“Index”,“Mil”,null,null)
@Nkrosi您建议的两个链接都不起作用。