<head>
<meta name="viewport" content="width=device-width" />
<title>Show</title>
@*
传输路径:@Url.Action("传输路径的方法")
指明样式类型:type="text/css"
*@
<link href="@Url.Action("/Css")" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<h1>用Show方法改变字体颜色:</h1>
<h3>获取到的值为:@ViewBag.color</h3>
<form action="/Default/Show" method="post">
<input type="radio" name="color" value="black" />黑色<br />
<input type="radio" name="color" value="red" />红色<br />
<input type="radio" name="color" value="blue" />蓝色<br />
<input type="submit" value="保存" />
</form>
</div>
</body>
public ActionResult Show()
{
return View();
}
[HttpPost]
public ActionResult Show(string color)
{
HttpCookie cookie = Request.Cookies["color"] ?? new HttpCookie("color", color);
cookie.Expires = DateTime.MaxValue;
ViewBag.color = color;
cookie.Value = color;
Response.SetCookie(cookie);
return View();
}
public ActionResult Css()
{
HttpCookie cookie = Request.Cookies["color"] ?? new HttpCookie("color", "black");
switch (cookie.Value)
{
case "red":
return Content("body{ color:red }", "text/css");
break;
case "blue":
return Content("body{ color:blue }", "text/css");
break;
default://前面都不执行,就执行它
return Content("body{ color:black }", "text/css");
break;
}
}