1·调用方法的另一种写法
private void Say1(string name,int age,string qq)
{
Console.WriteLine(name);
}
private void Say2()
{
//Say1("lxf");
Say1("lxf",18,"123");
Say1(age:18,name:"lxf",qq:"123");
}
下面也算 routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
name:代表路由规则名字,而且不能重复,把name当作键值对的名字。
url:路由规则,访问的路由格式。参数的名字要和路由规则id位置的名字一致例如:
localhost:1502/hello/edit/15 输出15
public ActionResult Edit(string id)
{
ViewBag.Id2 = Request["id"];//这里没有值,因此用路由规则没有放到request里面。
ViewBag.Id = id;//输出15
return View();
}
defaults:设置默认值,如果路由规则陪配上就会访问默认值的路径。
定义路由规则:
//自定义路由规则的要求:小范围写在前,大范围写在后
//2014-1-1-1
routes.MapRoute(
name: "NewsShow",
url: "{year}-{month}-{day}-{id}",
defaults: new
{
controller = "News",
action = "Show"
},
constraints: new//命名的约束
{
year = "^\\d{4}$",
month = "^\\d{1,2}$",
day = "^\\d{1,2}$"
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
localhost:1223/2014-1-1-99
public ActionResult Show(int id,int year)
{
ViewBag.Id = year;//输出2014 参数的名字要和路由规则一致才能输出
return View();
}
localhost:1223/2014-1-1111-99 第三个写出多余两位就会报错404匹配不到路由规则。
调试路由规则的代码加到Global里面:
页面显示如下: