ASP.NET MVC中多种ActionResult用法总结
最近一段时间做了个ASP.NET MVC4.0的项目,项目马上就要结束了,今天忙里偷闲简单总结一下心得:
1. 如果Action需要有返回值的话,必须是ActionResult的话,可以返回一个EmptyResult,Demo实例如下:
/// <summary>
///
/// </summary>
/// <returns>EmptyResult</returns>
public ActionResult EmptyActionResult()
{
Response.Write("This is a string content");
return new EmptyResult();
}
2. 如果需要转向指定的页面的话,转向一个指定的url,返回类型为RedirectResult,Demo实例如下:
/// <summary>
/// Controller.Redirect(),转向一个指定的url
/// </summary>
/// <returns>RedirectResult</returns>
public ActionResult RedirectResult()
{
return base.Redirect("~/Controllers/DestinationController");
}
3.如果需要转向一个指定的Action,返回类型为RedirectToRouteResult,Demo实例如下:
/// <summary>
/// Controller.Redirect(),转向一个指定的Action
/// </summary>
/// <returns>RedirectToRouteResult</returns>
public ActionResult RedirectToRouteResult()
{
return base.RedirectToAction("DestinationOController");
}
4.如果需要将指定的对象以JSON串的形式返回,返回类型为JsonResult,Demo实例如下:
/// <summary>
/// Controller.Json(),将对象以Json的形式返回
/// </summary>
/// <returns>JsonResult</returns>
public ActionResult GetJsonResult()
{ var jsonObj=new {Name:"Test Name";Age:};
return base.Json(jsonObj);
}
5. 如果需要返回一段Javascript代码,返回类型为JavascriptResult,Demo实例如下:
/// <summary>
/// Controller.JavaScript(),将对象以Javascript脚本的形式返回
/// </summary>
/// <returns>JavaScript</returns>
public ActionResult GetJavascriptResult()
{
return base.JavaScript("alert('This is JavaScript result')");
}
6. 如果需要输出一段指定的内容,返回类型为ContentResult,Demo实例如下:
/// <summary>
/// Controller.Content(),将对象以ContentResult的形式返回
/// </summary>
/// <returns>ContentResult</returns>
public ActionResult ContentResult()
{
string contentString = string.Format("<span style='color: red'>{0}</span>", "This is content result");
return base.Content(contentString);
}
7. 如果需要将一个文件以字节输入的格式输入,返回类型为FileContentResult,Demo实例如下:
/// <summary>
/// Controller.Content(),将对象以byte[]的形式返回
/// </summary>
/// <returns>FileContentResult</returns>
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, , length);
fs.Close();
return base.File(buffer, "image/gif"); }
8. 如果需要将一个文件以文件流的格式输出,返回类型为FileStreamResult,Demo实例如下:
/// <summary>
/// Controller.Content(),将对象以FileStream的形式返回
/// </summary>
/// <returns>FileStreamResult</returns>
public ActionResult FileStreamResult()
{
FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open);
return base.File(fs, @"image/gif");
}
9. 如果需要返回未经授权浏览状态的结果,返回类型为HttpUnauthorizedResult,Demo实例如下:
/// <summary>
/// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),
/// 如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页
/// </summary>
/// <returns>HttpUnauthorizedResult</returns>
public ActionResult HttpUnauthorizedResult()
{
return new HttpUnauthorizedResult();
}
10. 如果需要返回html页面的部分[.ascx],返回对象的类型为PartialViewResult,Demo实例如下:
/// <summary>
/// Controller.PartialView(),查找PartialView,即 .ascx 文件
/// </summary>
/// <returns>PartialView</returns>
public ActionResult PartialViewResult()
{
return base.PartialView();
}
11. 如果需要返回完整的Html,即完整的View,返回的类型了ViewResult,Demo实例如下:
/// <summary>
/// Controller.View(),查找View,即.aspx 文件
/// </summary>
/// <returns>ViewResult</returns>
public ActionResult ViewResult()
{
// 如果没有指定 View 名称,则寻找与 Action 名称相同的 View
return base.View();
}
12. 使用Get方式调用Action,返回类型为参数为空的ViewResult,Demo程序实例如下:
/// <summary>
/// 用于演示 Get 方式调用 Action
/// id 是根据路由过来的;param1和param2是根据参数过来的
/// </summary>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetDemo(int id, string param1, string param2)
{
ViewData["ID"] = id;
ViewData["Param1"] = param1;
ViewData["Param2"] = param2; return View();
}
14. 处理上传文件的Action,Demo实例如下:
/// <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中多种ActionResult用法总结的更多相关文章
- Asp.Net MVC中DropDownListFor的用法(转)
2016.03.04 扩展:如果 view中传入的是List<T>类型 怎么使用 DropList 既然是List<T> 那么我转化成 T List<T>的第一个 ...
- Asp.Net MVC中DropDownListFor的用法
在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...
- 转:Asp.Net MVC中DropDownListFor的用法
在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...
- 理解ASP.NET MVC中的ActionResult
通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...
- .NET MVC中的ActionResult
一 摘要 本文介绍了ASP.NET MVC中的ActionResult,本节主要介绍 EmptyResult / Content Result /JavaScriptResult /JsonResu ...
- ASP.NET MVC中Area的另一种用法
ASP.NET MVC中Area的另一种用法 [摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{ac ...
- ASP.NET MVC中常用的ActionResult类型
常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...
- .Net Mvc学习——ASP.NET MVC中常用的ActionResult类型
一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
随机推荐
- 在APACHE服务器上的访问方式上去除index.php
在APACHE服务器上的访问方式上去除index.php 下面我说下 apache 下 ,如何 去掉URL 里面的 index.php 例如: 你原来的路径是: localhost/index ...
- VS 2015 localhost访问有效 改用 IP访问 400错误 invalid hostname 修改方法
今天新起站点发现在Chrome浏览器中,通过localhost访问是有效的,但是通过本机IP甚至127.0.0.1访问无效, 报的错误是400 Bad Request Invalid HostName ...
- 掌握Redmine
一个带有建议.技巧和最佳实践的全面指导和易懂易学的结构. 掌握Redmine 版权©2013 Packt出版 前言(略) 1.熟悉Redmin 我们尝试去做一个新的网站应用程序的时候,回去询问一些了解 ...
- PHP的后期静态绑定
self 是个孝子 不管后来 谁是它的领导(调用它) 谁生了它 它就听谁的 子类调用父类的方法 self 的生存空间是父类 不管是不是子类调用 我生在哪 我就在哪个类里面找属性/方法 static ...
- c# 反射应用之工厂
反射是.net的核心功能,十分的强大.但是好像微软封装的太过了,作为程序员,在实际项目中我很少用到反射(估计是参加的大型项目太少了,需要交互第三方的项目太少了). 工厂模式是软件设计模式中重要的一种, ...
- React Native + Nodejs 使用RSA加密登录
想用rn做个RSA(非对称加密)登录 基本流程就是在服务端生成RSA后,将“公钥”发到客户端,然后客户端用“公钥”加密信息发送到服务端,服务务端用私钥解密. 过程不复杂,问题在于,nodejs和rn都 ...
- 【译】Permissions Best Practices Android M权限最佳做法
Permissions Best Practices PreviousNext In this document Consider Using an Intent Don't Overwhelm th ...
- Dom学习笔记
今天老师出了一道面试题目:取到表单里面的textbox的值,两种方法.知道一种,老师说的什么dom,我竟然不知道. 以前学html的时候,老师也重来没有提到dom的概念.javaScript只是学了一 ...
- golang中不定参数与数组切片的区别
package main import "fmt" func main() { myfunc1(, , , ) //传递不定数量的参数 myfunc2([], , , }) //传 ...
- gnome3.X添加开机启动项
背景:升级gnome后发现gnome-session-properties不见了,想把sslocal随机启动遇到了麻烦... 特别说明:此为图形桌面开机启动项,因此只有通过图形桌面登陆用户后才能启动. ...