URL.Action()包括路由值

hja*_*her 59 asp.net-mvc html-helper asp.net-mvc-4

我有一个ASP.Net MVC 4应用程序,我正在使用这样的Url.Action助手: @Url.Action("Information", "Admin")

此页面用于添加新的和编辑管理员配置文件.URL如下:

 Adding a new:       http://localhost:4935/Admin/Information
 Editing Existing:   http://localhost:4935/Admin/Information/5 <==Admin ID
Run Code Online (Sandbox Code Playgroud)

当我在Editing Existing网站的部分,并决定我想添加一个新的管理员时,我点击以下链接:

 <a href="@Url.Action("Information", "Admin")">Add an Admin</a>
Run Code Online (Sandbox Code Playgroud)

但问题是上面的链接实际上是这样的http://localhost:4935/Admin/Information/5.只有在我编辑现有管理员的页面时才会发生这种情况.网站上的其他任何地方都能正确链接到http://localhost:4935/Admin/Information

有没有人见过这个?

更新:

RouteConfig:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );    
Run Code Online (Sandbox Code Playgroud)

Ema*_*eiz 91

mvc中的传出URL基于当前路由模式生成.

因为您的信息操作方法需要id参数,并且您的路径集合具有当前请求的URL的ID(/ Admin/Information/5),所以id参数会自动从现有路径集合值​​中获取.

要解决此问题,您应该使用UrlParameter.Optional:

 <a href="@Url.Action("Information", "Admin", new { id = UrlParameter.Optional })">Add an Admin</a>
Run Code Online (Sandbox Code Playgroud)

  • 我们怎样才能在asp.net核心中做到这一点?没有`UrlParameter.Optional` (8认同)