gpt4 book ai didi

asp.net - UrlHelper.Action ("Edit", "Ad") 返回 id 参数?

转载 作者:行者123 更新时间:2023-12-05 01:22:50 25 4
gpt4 key购买 nike

你好,

我正在使用以下代码生成 URL:

UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
urlHelper.Action("Edit", "Ad");

如果我目前在 URL http://localhost:16055/Ad/Edit/87 上Action 方法将返回:“/Ad/Edit/87”?

为什么?我认为 urlHelper.Action("Edit", "Ad") 在此 satet 中不包含任何参数?

致以最诚挚的问候

编辑 1:(路线)

    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 }
);

最佳答案

I thought that urlHelper.Action("Edit", "Ad") would in this satet not include any parameters?

嗯,你想错了。所有 url 帮助程序都会自动包含所有作为原始请求一部分的参数。因此,如果您有一个 id 路由参数,它的值将被重用。

如果您不希望出现这种情况,则必须显式设置这些参数的值:

var action = urlHelper.Action("Edit", "Ad", new { id = "" });

关于asp.net - UrlHelper.Action ("Edit", "Ad") 返回 id 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9472009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com