ASP.NET MVC中Controller的使用Demo
2014-07-08 15:18
253 查看
ActionResult是ContentResult类、ViewResult类、JsonResult类等所有的Controller中返回值类型的基类
ASP.NET MVC中Controller的使用Demo
ASP.NET MVC中Controller的使用Demo
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using System.IO; namespace DemoRC.Controllers { public class DemoController : Controller { /// <summary> /// http://localhost:1847/Demo/ContentResultDemo /// </summary> /// <returns></returns> public ActionResult ContentResultDemo() { string contentString = "ContextResultDemo! 请查看 Controllers/DemoController.cs文件,里面包含所有类型ActionResult的用法."; return Content(contentString); } /// <summary> /// http://localhost:1847/Demo/EmptyResultDemo /// </summary> /// <returns></returns> public ActionResult EmptyResultDemo() { return new EmptyResult(); } /// <summary> /// http://localhost:1847/Demo/FileContentResultDemo /// </summary> /// <returns></returns> public ActionResult FileContentResultDemo() { FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read); byte[] buffer = new byte[Convert.ToInt32(fs.Length)]; fs.Read(buffer, 0, Convert.ToInt32(fs.Length) ); return File(buffer, @"image/gif"); } /// <summary> /// http://localhost:1847/Demo/FilePathResultDemo /// </summary> /// <returns></returns> public ActionResult FilePathResultDemo() { //可以将一个jpg格式的图像输出为gif格式 return File(Server.MapPath(@"/resource/Images/2.jpg"), @"image/gif"); } /// <summary> /// http://localhost:1847/Demo/FileStreamResultDemo /// </summary> /// <returns></returns> public ActionResult FileStreamResultDemo() { FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read); return File(fs, @"image/gif"); } /// <summary> /// http://localhost:1847/Demo/HttpUnauthorizedResultDemo /// </summary> /// <returns></returns> public ActionResult HttpUnauthorizedResultDemo() { return new HttpUnauthorizedResult(); } /// <summary> /// http://localhost:1847/Demo/JavaScriptResultDemo //mvc ajax脚本引入才会起作用 /// </summary> /// <returns></returns> public ActionResult JavaScriptResultDemo() { return JavaScript(@"alert(""Test JavaScriptResultDemo!"")"); } /// <summary> /// http://localhost:1847/Demo/JsonResultDemo /// </summary> /// <returns></returns> public ActionResult JsonResultDemo() { var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" }; return Json(tempObj); } /// <summary> /// http://localhost:1847/Demo/RedirectResultDemo /// </summary> /// <returns></returns> public ActionResult RedirectResultDemo() { return Redirect(@"http://localhost:1847/Demo/ContentResultDemo"); } /// <summary> /// http://localhost:1847/Demo/RedirectToRouteResultDemo /// </summary> /// <returns></returns> public ActionResult RedirectToRouteResultDemo() { return RedirectToAction(@"FileStreamResultDemo"); } /// <summary> /// http://localhost:1847/Demo/PartialViewResultDemo /// </summary> /// <returns></returns> public ActionResult PartialViewResultDemo() { return PartialView(); } /// <summary> /// http://localhost:1847/Demo/ViewResultDemo /// </summary> /// <returns></returns> public ActionResult ViewResultDemo() { //如果没有传入View名称, 默认寻找与Action名称相同的View页面. return View(); } } } </span>
相关文章推荐
- ASP.NET MVC搭建项目后台UI框架—5、Demo演示Controller和View的交互
- Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
- ASP.NET MVC使用UEditor,并与Controller共享
- Asp.net Mvc 的积木,谈Asp.net MVC中视图逻辑模块化和controller组件的使用方法
- ASP.NET MVC使用UEditor,并与Controller共享
- ASP.NET MVC使用ActionFilterAttribute实现权限限制的方法(附demo源码下载)
- Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
- ASP.NET MVC 3 中 Chart 的使用 Demo
- Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
- ASP.NET MVC 3 中 Chart 的使用 Demo
- ASP.NET Core开发-MVC 使用dotnet 命令创建Controller和View
- ASP.NET MVC 里redirectMode="ResponseRewrite" 时候无法使用 Controller 来设置特定的错误页面。
- 使用Asp.net MVC, Jquery, Jquery UI构建基于Ajax的RIA应用.(更新Demo下载)
- Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
- ASP.NET MVC 3 中 RemoteAttribute 的使用 Demo
- ASP.NET MVC:如何提供 Controller 继承体系使用的 ModelBinder?
- Asp.net Mvc Framework 十一 (自定义Helper在MVC中的使用)
- Asp.net Mvc Framework 十一 (自定义Helper在MVC中的使用)
- 使用Asp.net mvc + Linq + mvc_scaffold_gen_setup.exe 生成一个完整的家庭帐册大管家程序 之一
- Asp.net Mvc中MVCContrib中无法使用Castle的发解决方案