最近一段时间做了个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用法总结的更多相关文章

  1. Asp.Net MVC中DropDownListFor的用法(转)

    2016.03.04 扩展:如果 view中传入的是List<T>类型 怎么使用 DropList 既然是List<T> 那么我转化成 T  List<T>的第一个 ...

  2. Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  3. 转:Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  4. 理解ASP.NET MVC中的ActionResult

    通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...

  5. .NET MVC中的ActionResult

    一  摘要 本文介绍了ASP.NET MVC中的ActionResult,本节主要介绍 EmptyResult / Content Result /JavaScriptResult /JsonResu ...

  6. ASP.NET MVC中Area的另一种用法

    ASP.NET MVC中Area的另一种用法 [摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{ac ...

  7. ASP.NET MVC中常用的ActionResult类型

    常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...

  8. .Net Mvc学习——ASP.NET MVC中常用的ActionResult类型

    一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...

  9. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

随机推荐

  1. TortoiseGit 连接Git服务器不用每次输入用户名和密码的方法

    每次git push 都要输入用户名和密码. 虽然安全,但在自己电脑上每次都输有些麻烦,如何记住用户名和密码呢? 试了很多方法,找到这个最简单,亲测可行. 当你配置好git后,在C盘C:\Users\ ...

  2. mysql 查询当天、本周,本月,上一个月的数据

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 近7天 DAY) <= date(时间字段名) 近30天 DAY) & ...

  3. 多线程完成socket

    //服务器端代码 public class Service { //服务器 public static void main(String[] args) { ServerSocket serverSo ...

  4. spark 1.5.2配置记录

    1)slaves # A Spark Worker will be started on each of the machines listed below. dataNode 2)spark-env ...

  5. pct xcode7

    1.) 打开你的Xcode工程. 在Supporting Files目录下,选择 File > New > File > iOS > Other > PCH File 然 ...

  6. 转: JAVA递归算法实例小结

    一.递归算法设计的基本思想是: 对于一个复杂的问题,把原问题分解为若干个相对简单类同的子问题,继续下去直到子问题简单到能够直接求解,也就是说到了递推的出口,这样原问题就有递推得解. 在做递归算法的时候 ...

  7. JavaScript与java的异同(一)

    讲个故事:话说很久很久以前,有一个叫网景(Netscape)的,十月怀胎,他生了个儿子,很开兴,给儿子取名livescript.Livescript很勤奋,帮大叔大婶干了好多活,也给他爸赚了很多钱.突 ...

  8. 【Python⑥】python的缩进,条件判断和循环

    缩进 Python的最大特色是用缩进来标明成块的代码. 这点和其他语言区别很明显,比如大家熟悉的C语言里: ) { num+=; flag-=; } 而在python中: if flag>= 0 ...

  9. 在Spring tools suite中使用git 共享项目

    我们都在eclipse 和 myeclipse中使用过cvs 和 svn 版本控制工具进行团队开发,今天我学习了另外一种版本控制工具git,下面我演示如何在Spring tools suite中使用g ...

  10. Android 6.0 新特性

    首先谈一谈Android 6.0的一些新特性 锁屏下语音搜索 指纹识别 更完整的应用权限管理 Doze电量管理 Now onTap App link 在开发过程中与我们关系最密切的就是"更完 ...