Controllers and Actions
2012-10-17 23:47
316 查看
public class ExampleController : Controller { public ContentResult Index() { string message = "This is plain text"; return Content(message, "text/plain", Encoding.Default); } public RedirectToRouteResult Redirect() { return RedirectToRoute(new { controller = "Example", action = "Index", ID = "MyID" }); } public HttpStatusCodeResult StatusCode() { return HttpNotFound(); } public FileResult AnnualReport() { string filename = @"c:\AnnualReport.pdf"; string contentType = "application/pdf"; string downloadName = "AnnualReport2011.pdf"; return File(filename, contentType, downloadName); } public JavaScriptResult SayHello() { return JavaScript("alert('Hello, world!')"); } public RssActionResult RSS() { StoryLink[] stories = GetAllStories(); return new RssActionResult<StoryLink>("My Stories", stories, e => { return new XElement("item", new XAttribute("title", e.Title), new XAttribute("description", e.Description), new XAttribute("link", e.Url)); }); } public ActionResult Test() { return new SimplifiedRedirectResult("rss"); } [HttpPost] public JsonResult JsonData() { StoryLink[] stories = GetAllStories(); return Json(stories); } private StoryLink[] GetAllStories() { return new StoryLink[] { new StoryLink { Title = "First example story", Description = "This is the first example story", Url = "/Story/1"}, new StoryLink { Title = "Second example story", Description = "This is the second example story", Url = "/Story/2"}, new StoryLink { Title = "Third example story", Description = "This is the third example story", Url = "/Story/3"}, }; } } public class StoryLink { public string Title { get; set; } public string Description { get; set; } public string Url { get; set; } }
RssActionResult:
public abstract class RssActionResult : ActionResult { } public class RssActionResult<T> : RssActionResult { public RssActionResult(string title, IEnumerable<T> data, Func<T, XElement> formatter) { Title = title; DataItems = data; Formatter = formatter; } public IEnumerable<T> DataItems { get; set; } public Func<T, XElement> Formatter { get; set; } public string Title { get; set; } public override void ExecuteResult(ControllerContext context) { HttpResponseBase response = context.HttpContext.Response; // set the content type of the response response.ContentType = "application/rss+xml"; // get the RSS content string rss = GenerateXML(response.ContentEncoding.WebName); // write the content to the client response.Write(rss); } private string GenerateXML(string encoding) { XDocument rss = new XDocument(new XDeclaration("1.0", encoding, "yes"), new XElement("rss", new XAttribute("version", "2.0"), new XElement("channel", new XElement("title", Title), DataItems.Select(e => Formatter(e))))); return rss.ToString(); }
SimplifiedRedirectResult:
public SimplifiedRedirectResult(string url): this(url, permanent: false) { } public SimplifiedRedirectResult(string url, bool permanent) { Permanent = permanent; Url = url; } public bool Permanent { get; private set; } public string Url { get; private set; } public override void ExecuteResult(ControllerContext context) { string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext); context.Controller.TempData.Keep(); if (Permanent) { context.HttpContext.Response.RedirectPermanent( destinationUrl, endResponse: false); } else { context.HttpContext.Response.Redirect(destinationUrl, endResponse: false); } }
相关文章推荐
- Actions, Controllers and Results(翻译)
- ASP.NET MVC Controllers and Actions
- Asp.Net MVC 3【Controllers And Actions】
- ASP.NET MVC Controllers and Actions
- 【二】Actions, Controllers and Results
- ASP.NET MVC 5 Controllers and Actions
- Change Layout in Controllers and Actions in Zend Framework 2
- Actions, Controllers and Results【翻译】
- play framework2学习之旅<2.1>Actions, Controllers and Results
- 1.1_Actions, Controllers and Results
- ASP.NET MVC Controllers and Actions
- iOS - Actions and Outlets
- Dynamically Loading Controllers and Views with AngularJS/$controllerProvider and RequireJS
- [Mobx] Use MobX actions to change and guard state
- ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results
- A collection of iOS7 animation controllers and interaction controllers, providing flip, fold and all
- SharePoint Custom Actions Location and Sample
- Chapter 17 Autorotation, Popover Controller, and Modal View Controllers
- 三十而立,从零开始学ios开发(十六):Navigation Controllers and Table Views(下)
- Supra The sensation of high-class and extreme actions