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)/” 这样子的,当然这不是我们想要的格式. ...
随机推荐
- bzoj 1175: The stairways of Saharna
一道杨氏矩阵的题,萌新初入门,还不是很懂,这篇 blog 讲的超级好(就是看图有点麻烦) 据说这玩意儿可以代替堆和平衡树用,支持插入.删除.查询,跑得还挺快的(慢着,复杂度好像是 n^2 ? 而且空间 ...
- UR#13 SRAND
总感觉这位大仙讲的很清楚:bztminamoto 题意 题目讲的是求 l~r 内所有数的次大质因子,这里设 f(x) 为 x 的次大质因子 我们差分一下就变成求两个前缀和信息了 按照套路,我们考虑 S ...
- mongodb3.4.6配置主从
.rpm包安装mongodb3.4.6 下载地址:https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/RPMS/ rpm -ivh ...
- linux压缩与解压(持续更新)
压缩 tar cvzf w.tar.gz xxx1 xxx2 对应解压:tar xvzf w.tar.gz
- 不想用ssh框架
学过三遍多的样子,没有感悟到特别多的好处. 现在工作都用,想找一个不用这个的工作就不好找.c的话,觉得没有Java面向对象提炼得好. 不是很明白怎么都用,知道自己不想用.里边太多复杂和要背下来的东西, ...
- Confluence 6 数据导入和导出
Confluence 管理员和用户可以从各种方法向 Confluence 中导入数据.针对不同的导入方式,有关权限的要求也是不相同的.请参考页面 Import Content Into Conflu ...
- Confluence 6 导入模板的步骤
第一步:检查你 Confluence 站点中安装的模板组件 查看当前已经导入到你 Confluence 站点中可用的模板组件: 以系统管理员或者 Confluence 管理员权限登录 Confluen ...
- openmp
https://blog.csdn.net/fuwenyan/article/details/79500765a https://www.cnblogs.com/yangyangcv/archive/ ...
- git使用中出现的错误
因同时有两个git账户,之前登录了git A 用户在使用了 1. 长期存储密码 git config --global credential.helper store 之后在git B 账 ...
- gnuradio 初次使用
参考链接: 入门 http://www.cnblogs.com/moon1992/p/5739027.html 创建模块 http://www.cnblogs.com/moon1992/p/54246 ...