UrlHelper.Action(“Edit”, “Ad”) returns id parameter?

别说谁变了你拦得住时间么 提交于 2020-01-13 19:12:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!