了解ASP.NET MVC几种ActionResult的本质:EmptyResult & ContentResult

原文:http://www.cnblogs.com/artech/archive/2012/08/13/action-result-01.html

 public class HomeController : Controller
    {
        //其他成员
        public ActionResult Css()
        {
            HttpCookie cookie = Request.Cookies["theme"] ?? new HttpCookie("theme", "default");
            switch (cookie.Value)
            {
                case "Theme1": return Content("body{font-family: SimHei; font-size:1.2em}", "text/css");
                case "Theme2": return Content("body{font-family: KaiTi; font-size:1.2em}", "text/css");
                default: return Content("body{font-family: SimSong; font-size:1.2em}", "text/css");
            }
        }
    }
public class HomeController : Controller
    {
        //其他成员
        public ActionResult Index()
        {
            HttpCookie cookie = Request.Cookies["theme"] ?? new HttpCookie("theme", "default");
            ViewBag.Theme = cookie.Value;
            return View();
        }

        [HttpPost]
        public ActionResult Index(string theme)
        {
            HttpCookie cookie = new HttpCookie("theme", theme);
            cookie.Expires = DateTime.MaxValue;
            Response.SetCookie(cookie);
            ViewBag.Theme = theme;
            return View();
        }
    }
<html>
<head>
    <title>主题设置</title>    
    <link type="text/css"  rel="Stylesheet" href="@Url.Action("Css")" />
</head>
<body>
    @using(Html.BeginForm())
    {
        string theme = ViewBag.Theme.ToString();        
        @Html.RadioButton("theme", "Default", theme == "Default")<span>默认主题(宋体)</span><br/>
        @Html.RadioButton("theme", "Theme1", theme == "Theme1")<span>主题1(黑体)</span><br/>
        @Html.RadioButton("theme", "Theme2", theme == "Theme2")<span>主题2(楷体)</span><br />
        <input type="submit" value="保存" />
    }
</body>
</html>


你可能感兴趣的:(Url.Action,ContentResult)