问题
Hi,
I am using the following code to generate a URL :
UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
urlHelper.Action("Edit", "Ad");
If Im currently is on URL http://localhost:16055/Ad/Edit/87 the Action method will return : "/Ad/Edit/87" ?
Why? I thought that urlHelper.Action("Edit", "Ad") would in this satet not include any parameters?
BestRegards
Edit 1: (routs)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Ad", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"TreeEditing", // Route name
"{controller}/{action}/{name}/{id}", // URL with parameters
new { controller = "AdCategory", action = "Add", name = string.Empty, id = -1 }
);
回答1:
I thought that urlHelper.Action("Edit", "Ad") would in this satet not include any parameters?
Well, you thought wrong. All url helpers automatically include all arguments that were part of the original request. So if you had an id route parameter present, its value will be reused.
If you don't want this behavior you will have to explicitly set the values for those parameters:
var action = urlHelper.Action("Edit", "Ad", new { id = "" });
来源:https://stackoverflow.com/questions/9472009/urlhelper-actionedit-ad-returns-id-parameter