gpt4 book ai didi

c# - 将多个参数发送到 ASP.NET MVC 中的操作

转载 作者:IT王子 更新时间:2023-10-29 04:37:24 25 4
gpt4 key购买 nike

我想将多个参数发送到 ASP.NET MVC 中的一个操作。我还希望 URL 看起来像这样:

http://example.com/products/item/2

代替:

http://example.com/products/item.aspx?id=2

我也想为发件人做同样的事情,这是当前的 URL:

http://example.com/products/item.aspx?id=2&sender=1

我如何在 ASP.NET MVC 中使用 C# 完成这两项工作?

最佳答案

如果您可以在查询字符串中传递内容,那将非常简单。只需更改 Action 方法以获取具有匹配名称的附加参数:

// Products/Item.aspx?id=2 or Products/Item/2
public ActionResult Item(int id) { }

会变成:

// Products/Item.aspx?id=2&sender=1 or Products/Item/2?sender=1
public ActionResult Item(int id, int sender) { }

ASP.NET MVC 将为您完成所有连接工作。

如果您想要一个干净的 URL,只需将新路由添加到 Global.asax.cs:

// will allow for Products/Item/2/1
routes.MapRoute(
"ItemDetailsWithSender",
"Products/Item/{id}/{sender}",
new { controller = "Products", action = "Item" }
);

关于c# - 将多个参数发送到 ASP.NET MVC 中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586336/

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