asp.net mvc5 action多个参数

需要完成http://site.com/user/add/1/2这样的url解析

使用action的参数直接获取数据的方式

Action声明如下

 public ActionResult Add(int id, int run=0)
        {
            ViewBag.clubID = id;
            if(run == 0)
                return View();
            else
                return View(exectudeAdd(id));
        }

只需要对路由进行添加即可完成

注意观察,我们需要将参数名对应到路由中,即此处的id和run,参数如上述action参数列表

context.MapRoute(
                "student_new",
                "student/{controller}/{action}/{id}/{run}",
                new { action = "Index", id = UrlParameter.Optional, run = UrlParameter.Optional }
            );
            context.MapRoute(
                "student_default",
                "student/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

留待后查同时方便别人

2017.12.10 1:03

原文地址:https://www.cnblogs.com/ives/p/8014502.html