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)/” 这样子的,当然这不是我们想要的格式. ...
随机推荐
- MySQL--详细查询操作(单表记录查询、多表记录查询(连表查询)、子查询)
一.单表查询 1.完整的语法顺序(可以不写完整,其次顺序要对) (不分组,且当前表使用聚合函数: 当前表为一组,显示统计结果 ) select distinct [*,查询字段1,查询字段2,表达式, ...
- elasticsearch5.0.1集群排错的几个思路总结
1.首先查看集群整体健康状态 # curl -XGET http://10.27.35.94:9200/_cluster/health?pretty { "cluster_name" ...
- centos6.5环境wget报错Unable to establish SSL connection
centos6.5环境wget报错Unable to establish SSL connection [root@centossz008 src]# wget --no-check-certific ...
- 开通博客的第一天上传我的C#基础笔记。
1.索引器 string arrStr = "sddfdfgfh"; 索引器的目的就是为了方便而已,可以在该类型的对象后面直接写[]访问该对象里面的成员 Console.Wr ...
- 点9图 Android设计中如何切图.9.png
转载自:http://blog.csdn.net/buaaroid/article/details/51499516 本文主要介绍如何制作 切图.9.png(点9图),另一篇姊妹篇文章Android屏 ...
- ACM-ICPC 2018 焦作赛区网络预赛 I Save the Room
Bob is a sorcerer. He lives in a cuboid room which has a length of AAA, a width of BBB and a height ...
- Confluence 6 配置 Windows 服务
当你使用 Start Confluence Automatically on Windows as a Service 的方式启动的时候,你有下面 2 种方式来配置你的系统属性:通过 command ...
- 从 Confluence 5.3 及其早期版本中恢复空间
如果你需要从 Confluence 5.3 及其早期版本中的导出文件恢复到晚于 Confluence 5.3 的 Confluence 中的话.你可以使用临时的 Confluence 空间安装,然后将 ...
- Confluence 6 安装 PostgreSQL
如果你的系统中还没有安装 PostgreSQL 数据库,你需要先下载后进行安装. 在安装 PostgreSQL 时候的一些小经验: 在安装的时候提供的 密码(password )是针对 'postg ...
- mongoDB基础使用
环境交代 操作系统: CentOS 6.8 64位 mongodb: 4.06 安装 官方下载地址:https://www.mongodb.org/dl/linux/x86_64-rhel62 阿里云 ...