比如:
route.MapRoute(
"myRoute",
"{controller}\{action}\{id}",
new {controller ="Home",action="Index“,id={"defaultid"}
);
问题是:这个{id},可以定义多个吗?如下做法可以吗?
route.MapRoute(
"myRoute",
"{controller}\{action}\{id}\{dd}",
new {controller ="Home",action="Index“,id={"defaultid"},dd={"dd"}
);
如果再考虑controller中的action方法的参数,是不是说如果一个id,action方法就一个参数,如果定义了两个,action方法就两个参数?
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
public ActionResult Demo(string id, string dd)
{
return View();
}
}
}
要这样写:
route.MapRoute(
"myRoute",
"{controller}\{action}\{id}\{dd}",
new {controller ="Home",action="Index“,id="defaultid",dd="dd"
);
第二个问题:
你访问Index action用 /home/index/1/
你访问Demo action用/home/demo/1/2/
http://list.jd.com/detail/5025-5026-12094.html
那么多大型网站中,个人是喜欢jd网站的url。jd网站的url,干净简洁,不像tmall或taobao等,后面老是跟一大堆的参数,如:
http://yao.tmall.com/?spm=3.7396704.20000006.10.zM3HVL&acm=tt-1141523-40928.1003.8.138801&uuid=138801&abtest=&scm=1003.8.tt-1141523-40928.OTHER_1416895448432_138801&pos=10
对于jd的url,用Asp.net mvc来写路由,那么可以写为:
context.MapRoute(
"list",
"Detail/{CategoryId1}-{CategoryId2}-{CategoryId3}.html",
new
{
controller = "List",
action = "Detail",
CategoryId1 = @"(\d)+",
CategoryId2 = @"(\d)+",
CategoryId3= @"(\d)+"
},
null,
new string[] { "xxxx.Controllers.WebSite" }
);
那么在Action中,我们可以写为:
public ActionResult List(int CategoryId1,nt CategoryId2,nt CategoryId3)
{
//do something
return View();
}
所以,你想写几个参数,就可以写几个参数
但一个url通常又会跟很多的参数,就像前面的天猫URL,那么这时,在Route与Action中,就可以不用动了,而想获取?后面的参数,则可以写为:
public ActionResult List(int CategoryId1,nt CategoryId2,nt CategoryId3)
{
//do something
string version=Request.QueryString["Version"];
string spm=Request.QueryString["spm"];
return View();
}