URL.Action(),包括路由值 [英] URL.Action() including route values
问题描述
我有一个ASP.Net MVC 4应用程序,现在用的是Url.Action助手这样的: @ Url.Action(信息,管理)
本页面同时用于增加新的编辑和管理员的配置文件。网址如下:
添加新:HTTP://本地主机:4935 /管理/信息
编辑现有:HTTP://本地主机:4935 /管理/信息/ 5℃==管理员ID
当我在网站的编辑现有
部分,并决定我想补充一个新的管理员我点击以下链接:
< A HREF =@ Url.Action(信息,管理员)>添加一个管理员< / A>
但问题是,上面的链接实际上是将的http://本地主机:4935 /管理/信息/ 5
。这只有当我在该页面编辑现有的管理是发生了。在网站上其他任何地方都可以正确链接到的http://本地主机:4935 /管理/信息
有其他人看到这一点?
更新:
RouteConfig:
routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
在MVC传出URL生成的基于当前的路由架构。
由于您的信息的操作方法需要id参数,和你的路由集合了当前的请求的URL的ID(/管理/信息/ 5),从现有的路由集合值自动得到。id参数
要解决这个问题,你应该使用UrlParameter.Optional:
< A HREF =@ Url.Action(信息,管理,新{ID = UrlParameter.Optional})>添加一个管理员< / A>
I have an ASP.Net MVC 4 app and am using the Url.Action helper like this: @Url.Action("Information", "Admin")
This page is used for both adding a new and edit an admin profile. The URLs are as follows:
Adding a new: http://localhost:4935/Admin/Information
Editing Existing: http://localhost:4935/Admin/Information/5 <==Admin ID
When I'm in the Editing Existing
section of the site and decide that I would like to add a new admin I click on the following link:
<a href="@Url.Action("Information", "Admin")">Add an Admin</a>
The problem however that the above link is actually going to http://localhost:4935/Admin/Information/5
. This only happens when I'm in that page editing an existing admin. Anywhere else on the site it links correctly to http://localhost:4935/Admin/Information
Has anyone else seen this?
UPDATE:
RouteConfig:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
outgoing url in mvc generated based on the current routing schema.
because your Information action method require id parameter, and your route collection has id of your current requested url(/Admin/Information/5), id parameter automatically gotten from existing route collection values.
to solve this problem you should use UrlParameter.Optional:
<a href="@Url.Action("Information", "Admin", new { id = UrlParameter.Optional })">Add an Admin</a>
这篇关于URL.Action(),包括路由值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!