最近一段时间做了个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. 基于PNotify的消息提示Demo(轮询)

    需求:有些任务需要定时更新,获取最新的消息,这样就需要定时轮询,再者需要一种友好的提示. 以下就是使用PNotify插件的消息提示: 1.HTML代码 <!DOCTYPE html> &l ...

  2. [深度优先搜索] POJ 1426 Find The Multiple

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28550   Accepted: 118 ...

  3. Coding源码学习第三部分(EaseStartView.m)

    首先接上篇的要做一个NSEnumerator 类的延展阅读. 枚举(NSEnumerator) (1)依附于集合类(NSArray,NSSet,NSDictionary),没有用来创建实例的接口. ( ...

  4. C#数据库导出(入)TXT

    导出: public void ExportTxt() { var file = System.IO.File.Open(path, System.IO.FileMode.Open); using ( ...

  5. onethinkp导入excel

    /** * Excel导入函数 * @author crx349 */ if (!empty($_FILES)) { $config = array( 'maxSize' => 3145728, ...

  6. android学习之ListView

    移通152余继彪 该组件用于显示列表的view  包含了三个关键元素 listView 适配器 以及数据,适配器主要是用于将数据映射到listview,适配器数据主要是有hasmap 配合list对每 ...

  7. C#如何获取项目中的其他文件夹的路径

    //一般用string p=AppDomain.CurrentDomain.BaseDirectory+"\\其他"; //其它的还有 string str1 =Process.G ...

  8. Tomcat配置错误导致Quartz执行两次问题

    以下基于tomcat服务器 我们通常将域名映射到指定服务器的端口上,以通过域名直接访问服务,如http://www.abc.com域名已绑定到本机的80端口,项目名wechat,则直接访问http:/ ...

  9. c# 第一个实例 通哥

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. HelloWorld Makefile Template

    DEPDIR = build_dep TARGET_NAME = helloworld CFLAGS = -Wall SRCS = main.c SRCS += foo.c OBJS = $(SRCS ...