ASP.NET MVC3 通过URL路由传多个参数方法

在Global.asax文件中,默认路由如下。

routes.MapRoute(
  "Default", // 路由名称
  "{controller}/{action}/{id}", // 带有参数的 URL
  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
);

方法一:

Url传参是通过Get的方式,一般我们都是通过一定规则的Url来传参。比如下面的URL。

http://localhost/contorller/action/id?Params1=a&Params2=b

注意:URL里面的“?”不能去掉

我们可以在controller中通过绑定方法的方式来进行获取,代码如下:

public ActionResult Index(ExpModel model, string id string Params1 , string Params2)
{
  ViewBag.id = id;
  ViewBag.P1 = Params1;
  ViewBag.P2= Params2;
  return View();
}

方法二:

修改MVC3中的路由规则

在Global.asax.cs中,修改路由规则:

routes.MapRoute(
  "Default1", // 路由名称
  "{controller}/{action}/{id}/{Parma1}/{Parma2}", // 带有参数的 URL   
  new { controller = "Home", action = "Index", id = UrlParameter.Optional, Parma1 = UrlParameter.Optional, Parma2 = UrlParameter.Optional} // 参数默认值
);

如访问的URL:http://localhost/contorller/action/id/a/b

获取和上面的方法一样。

 

posted @ 2015-09-17 15:58  [在路上ing]  阅读(1535)  评论(0编辑  收藏  举报