I'd like to send multiple parameters to an action in ASP.NET MVC. I'd also like the URL to look like this:
我想将多个参数发送到ASP中的一个动作。净MVC。我还希望URL是这样的:
http://example.com/products/item/2
instead of:
而不是:
http://example.com/products/item.aspx?id=2
I'd like to do the same for sender as well, here's the current URL:
我也想对发件人做同样的事情,这是当前的URL:
http://example.com/products/item.aspx?id=2&sender=1
How do I accomplish both with C# in ASP.NET MVC?
如何在ASP中实现c#。净MVC吗?
3 个解决方案
#1
26
If you're ok with passing things in the query string, it's quite easy. Simply change the Action method to take an additional parameter with a matching name:
如果您可以在查询字符串中传递信息,那么这很简单。只需更改操作方法,使其带有匹配名称的附加参数为:
// Products/Item.aspx?id=2 or Products/Item/2
public ActionResult Item(int id) { }
Would become:
将成为:
// Products/Item.aspx?id=2&sender=1 or Products/Item/2?sender=1
public ActionResult Item(int id, int sender) { }
ASP.NET MVC will do the work of wiring everything up for you.
ASP。NET MVC将为您完成连接所有内容的工作。
If you want a clean looking URL, you simply need to add the new route to Global.asax.cs:
如果你想要一个干净的URL,你只需要添加新的路径到Global.asax.cs:
// will allow for Products/Item/2/1
routes.MapRoute(
"ItemDetailsWithSender",
"Products/Item/{id}/{sender}",
new { controller = "Products", action = "Item" }
);
#2
12
If you want a pretty URL, then add the following to your global.asax.cs
.
如果您想要一个漂亮的URL,请在global.asax.cs中添加以下内容。
routes.MapRoute("ProductIDs",
"Products/item/{id}",
new { controller = Products, action = showItem, id="" }
new { id = @"\d+" }
);
routes.MapRoute("ProductIDWithSender",
"Products/item/{sender}/{id}/",
new { controller = Products, action = showItem, id="" sender="" }
new { id = @"\d+", sender=@"[0-9]" } //constraint
);
And then to use the needed actions:
然后采取必要的行动:
public ActionResult showItem(int id)
{
//view stuff here.
}
public ActionResult showItem(int id, int sender)
{
//view stuff here
}
#3
4
you can use any route rule for example:
你可以使用任何路线规则,例如:
{controller}/{action}/{param1}/{param2}
also you can use get params like :baseUrl?param1=1¶m2=2
也可以使用get params,比如:baseUrl?param1=1¶m2=2
and check this link, i hope it will help you.
看看这个链接,我希望它能帮到你。
#1
26
If you're ok with passing things in the query string, it's quite easy. Simply change the Action method to take an additional parameter with a matching name:
如果您可以在查询字符串中传递信息,那么这很简单。只需更改操作方法,使其带有匹配名称的附加参数为:
// Products/Item.aspx?id=2 or Products/Item/2
public ActionResult Item(int id) { }
Would become:
将成为:
// Products/Item.aspx?id=2&sender=1 or Products/Item/2?sender=1
public ActionResult Item(int id, int sender) { }
ASP.NET MVC will do the work of wiring everything up for you.
ASP。NET MVC将为您完成连接所有内容的工作。
If you want a clean looking URL, you simply need to add the new route to Global.asax.cs:
如果你想要一个干净的URL,你只需要添加新的路径到Global.asax.cs:
// will allow for Products/Item/2/1
routes.MapRoute(
"ItemDetailsWithSender",
"Products/Item/{id}/{sender}",
new { controller = "Products", action = "Item" }
);
#2
12
If you want a pretty URL, then add the following to your global.asax.cs
.
如果您想要一个漂亮的URL,请在global.asax.cs中添加以下内容。
routes.MapRoute("ProductIDs",
"Products/item/{id}",
new { controller = Products, action = showItem, id="" }
new { id = @"\d+" }
);
routes.MapRoute("ProductIDWithSender",
"Products/item/{sender}/{id}/",
new { controller = Products, action = showItem, id="" sender="" }
new { id = @"\d+", sender=@"[0-9]" } //constraint
);
And then to use the needed actions:
然后采取必要的行动:
public ActionResult showItem(int id)
{
//view stuff here.
}
public ActionResult showItem(int id, int sender)
{
//view stuff here
}
#3
4
you can use any route rule for example:
你可以使用任何路线规则,例如:
{controller}/{action}/{param1}/{param2}
also you can use get params like :baseUrl?param1=1¶m2=2
也可以使用get params,比如:baseUrl?param1=1¶m2=2
and check this link, i hope it will help you.
看看这个链接,我希望它能帮到你。