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

来源:互联网 发布:淘宝网旺旺在哪里 编辑:程序博客网 时间:2024/04/28 20:12

原文: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>


0 0