Asp.Net Mvc 返回类型总结
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Mvc.Ajax;
- using System.IO;
- namespace MVC.Controllers
- {
- /// <summary>
- /// Controller 类必须以字符串 "Controller" 做类名称的结尾,字符串 Controller 之前的字符串为 Controller 的名称,类中的方法名为 Action 的名称
- /// </summary>
- public class ControllerDemoController : Controller
- {
- // [NonAction] - 当前方法仅为普通方法,不解析为 Action
- // [AcceptVerbs(HttpVerbs.Post)] - 声明 Action 所对应的 http 方法
- /// <summary>
- /// Action 可以没有返回值
- /// </summary>
- public void Void()
- {
- Response.Write(string.Format("<span style='color: red'>{0}</span>", "void"));
- }
- /// <summary>
- /// 如果 Action 要有返回值的话,其类型必须是 ActionResult
- /// EmptyResult - 空结果
- /// </summary>
- public ActionResult EmptyResult()
- {
- Response.Write(string.Format("<span style='color: red'>{0}</span>", "EmptyResult"));
- return new EmptyResult();
- }
- /// <summary>
- /// Controller.Redirect() - 转向一个指定的 url 地址
- /// 返回类型为 RedirectResult
- /// </summary>
- public ActionResult RedirectResult()
- {
- return base.Redirect("~/ControllerDemo/ContentResult");
- }
- /// <summary>
- /// Controller.RedirectToAction() - 转向到指定的 Action
- /// 返回类型为 RedirectToRouteResult
- /// </summary>
- public ActionResult RedirectToRouteResult()
- {
- return base.RedirectToAction("ContentResult");
- }
- /// <summary>
- /// Controller.Json() - 将指定的对象以 JSON 格式输出出来
- /// 返回类型为 JsonResult
- /// </summary>
- public ActionResult JsonResult(string name)
- {
- System.Threading.Thread.Sleep(1000);
- var jsonObj = new { Name = name, Age = new Random().Next(20, 31) };
- return base.Json(jsonObj);
- }
- /// <summary>
- /// Controller.JavaScript() - 输出一段指定的 JavaScript 脚本
- /// 返回类型为 JavaScriptResult
- /// </summary>
- public ActionResult JavaScriptResult()
- {
- return base.JavaScript("alert('JavaScriptResult')");
- }
- /// <summary>
- /// Controller.Content() - 输出一段指定的内容
- /// 返回类型为 ContentResult
- /// </summary>
- public ActionResult ContentResult()
- {
- string contentString = string.Format("<span style='color: red'>{0}</span>", "ContentResult");
- return base.Content(contentString);
- }
- /// <summary>
- /// Controller.File() - 输出一个文件(字节数组)
- /// 返回类型为 FileContentResult
- /// </summary>
- public ActionResult FileContentResult()
- {
- FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
- int length = (int)fs.Length;
- byte[] buffer = new byte[length];
- fs.Read(buffer, 0, length);
- fs.Close();
- return base.File(buffer, "image/gif");
- }
- // <summary>
- /// Controller.File() - 输出一个文件(文件地址)
- /// 返回类型为 FileContentResult
- /// </summary>
- public ActionResult FilePathResult()
- {
- var path = Request.PhysicalApplicationPath + "Content/loading.gif";
- return base.File(path, "image/gif");
- }
- // <summary>
- /// Controller.File() - 输出一个文件(文件流)
- /// 返回类型为 FileContentResult
- /// </summary>
- public ActionResult FileStreamResult()
- {
- FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
- return base.File(fs, @"image/gif");
- }
- /// <summary>
- /// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页
- /// </summary>
- public ActionResult HttpUnauthorizedResult()
- {
- return new HttpUnauthorizedResult();
- }
- /// <summary>
- /// Controller.PartialView() - 寻找 View ,即 .ascx 文件
- /// 返回类型为 PartialViewResult
- /// </summary>
- public ActionResult PartialViewResult()
- {
- return base.PartialView();
- }
- /// <summary>
- /// Controller.View() - 寻找 View ,即 .aspx 文件
- /// 返回类型为 ViewResult
- /// </summary>
- public ActionResult ViewResult()
- {
- // 如果没有指定 View 名称,则寻找与 Action 名称相同的 View
- return base.View();
- }
- /// <summary>
- /// 用于演示处理 JSON 的
- /// </summary>
- public ActionResult JsonDemo()
- {
- return View();
- }
- /// <summary>
- /// 用于演示上传文件的
- /// </summary>
- public ActionResult UploadDemo()
- {
- return View();
- }
- /// <summary>
- /// 用于演示 Get 方式调用 Action
- /// id 是根据路由过来的;param1和param2是根据参数过来的
- /// </summary>
- [AcceptVerbs(HttpVerbs.Get)]
- public ActionResult GetDemo(int id, string param1, string param2)
- {
- ViewData["ID"] = id;
- ViewData["Param1"] = param1;
- ViewData["Param2"] = param2;
- return View();
- }
- /// <summary>
- /// 用于演示 Post 方式调用 Action
- /// </summary>
- /// <remarks>
- /// 可以为参数添加声明,如:[Bind(Include = "xxx")] - 只绑定指定的属性(参数),多个用逗号隔开
- /// [Bind(Exclude = "xxx")] - 不绑定指定的属性(参数),多个用逗号隔开
- /// [Bind] 声明同样可以作用于 class 上
- /// </remarks>
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult PostDemo(FormCollection fc)
- {
- ViewData["Param1"] = fc["param1"];
- ViewData["Param2"] = fc["param2"];
- // 也可以用 Request.Form 方式获取 post 过来的参数
- // Request.Form 内的参数也会映射到同名参数。例如,也可用如下方式获取参数
- // public ActionResult PostDemo(string param1, string param2)
- return View("GetDemo");
- }
- /// <summary>
- /// 处理上传文件的 Action
- /// </summary>
- /// <param name="file1">与传过来的 file 类型的 input 的 name 相对应</param>
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult UploadFile(HttpPostedFileBase file1)
- {
- // Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数
- // HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase;
- string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload", Path.GetFileName(file1.FileName));
- file1.SaveAs(targetPath);
- return View("UploadDemo");
- }
- }
- }
Asp.Net Mvc 返回类型总结的更多相关文章
- ASP.NET MVC 描述类型(二)
ASP.NET MVC 描述类型(二) 前言 上个篇幅中说到ControllerDescriptor类型的由来过程,对于ControllerDescriptor类型来言ActionDescriptor ...
- ASP.NET MVC 描述类型(一)
ASP.NET MVC 描述类型(一) 前言 在前面的好多篇幅中都有提到过ControllerDescriptor类型,并且在ASP.NET MVC 过滤器(一)篇幅中简单的描述过,今天我们就来讲一下 ...
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
问题背景: 在使用asp.net mvc 结合jquery esayui做一个系统,但是在使用使用this.json方法直接返回一个json对象,在列表中显示时发现datetime类型的数据在转为字符 ...
- 解决ASP.NET MVC返回的JsonResult 中 日期类型数据格式问题,和返回的属性名称转为“驼峰命名法”和循环引用问题
DateTime类型数据格式问题 问题 在使用ASP.NET MVC 在写项目的时候发现,返回给前端的JSON数据,日期类型是 Date(121454578784541) 的格式,需要前端来转换一下才 ...
- Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据
我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...
- ASP.NET MVC 枚举类型转LIST CONTROL控件
在实际应用中,我们经常会用到下拉框.多选.单选等类似的控件,我们可以统称他们为List Control,他们可以说都是一种类型的控件,相同之处都是由一个或一组键值对的形式的数据进行绑定渲染而成的. 这 ...
- ASP.NET MVC 中使用JavaScriptResult asp.net mvc 返回 JavaScript asp.mvc 后台返回js
return this.Content("<script>alert('暂无!');window.location.href='/Wap/Index';</script&g ...
- ASP .NET Controller返回类型
返回类型 return View(model); 即返回htmlreturn Json("String"); 返回Json格式的数据return File(new byte[] { ...
- 用JS解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
当用ajax异步时,返回JsonResult格式的时候,发现当字段是dateTime类型时,返回的json格式既然是“/Date(1435542121135)/” 这样子的,当然这不是我们想要的格式. ...
随机推荐
- 鸟哥Linux私房菜基础学习篇学习笔记1
鸟哥Linux私房菜基础学习篇学习笔记1 第三章 主导分区(MBR),当系统在开机的时候会主动去读取这个区块的内容,必须对硬盘进行分区,这样硬盘才能被有效地使用. 所谓的分区只是针对64Bytes的分 ...
- mysql:赋予用户权限、查看及修改端口号
一.mysql 赋给用户权限 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 即用u ...
- 前端 -----函数和伪数组(arguments)
函数 函数:就是将一些语句进行封装,然后通过调用的形式,执行这些语句. 函数的作用: 将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动. 简化编程,让编程模块化. ...
- 数据库-mysql-DDL-表记录操作
- linux软件安装、rpm操作命令、本地yum配置(有什么用)
1.yum是什么? yum的全称是yellow dog updater,modified,是一个shell前端软件包管理器;基于RPM包管理,能够从指定的服务器下载RPM包并自动安装,可以自动处理依赖 ...
- WinSCP安装与使用
WinSCP 是一个 Windows 环境下使用的 SSH(Source Shell)的开源图形化 SFTP(SSH File Transfer Protocol) 客户端.同时支持 SCP(So ...
- SQL Server 数据恢复到指点时间点(完整恢复)
SQL Server 数据恢复到指点时间点(完整恢复) 高文龙关注2人评论944人阅读2017-03-20 12:57:12 SQL Server 数据恢复到指点时间点(完整恢复) 说到数据库恢复,其 ...
- Confluence 6 自定义管理员联系信息
你可以自定义在 联系站点管理员(Contact Site Administrators)页面中显示的消息. 希望编辑这个管理员联系消息: 在屏幕的右上角单击 控制台按钮 ,然后选择 General C ...
- Confluence 6 修改你站点的外观和感觉
你可以为你的 Confluence 整个站点修改表现以及外观和感觉,也可以为单独的空间进行修改. 对整个站点进行的修改将会对使用全局外观和感觉(look and feel)的空间一并进行修改.如果某个 ...
- J2SE基础小结
1. 九种基本数据类型的大小,以及他们的封装类. 类型 基本类型 大小(字节) 默认值 封装类 整数型 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 ...