从抽象类Controller 的定义可以看出他 同时实现了 IAsyncController, IController

   public abstract class Controller : ControllerBase, IActionFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IController, IAsyncManagerContainer
{
protected Controller();
}

IAsyncController 就表示异步。

通过属性 DisableAsyncSupport 属性来判断是同步还是异步执行  DisableAsyncSupport=true 表示同步 DisableAsyncSupport=false 表示异步。

但是即使执行BeginExecute/EndExecute 方法,Controller也不一定是 异步方式执行,如下代码片段。

public class HomeController : Controller
{
protected override bool DisableAsyncSupport
{
get { return false; }
} public new HttpResponse Response
{
get { return System.Web.HttpContext.Current.Response; }
} /// <summary>
/// 同步执行
/// </summary>
/// <param name="requestContext"></param>
protected override void Execute(RequestContext requestContext)
{
Response.Write("Execute(); <br/>");
base.Execute(requestContext);
} /// <summary>
/// 同步执行
/// </summary>
protected override void ExecuteCore()
{
Response.Write("ExecuteCore(); <br/>");
base.ExecuteCore();
} /// <summary>
/// 同步异步都会执行
/// </summary>
protected override IAsyncResult BeginExecute(RequestContext requestContext,
AsyncCallback callback, object state)
{
Response.Write("BeginExecute(); <br/>");
return base.BeginExecute(requestContext, callback, state);
} /// <summary>
/// 同步异步都会执行
/// </summary>
/// <param name="asyncResult"></param>
protected override void EndExecute(IAsyncResult asyncResult)
{
Response.Write("EndExecute(); <br/>");
base.EndExecute(asyncResult);
} /// <summary>
/// 异步执行
/// </summary>
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback,
object state)
{
Response.Write("BeginExecuteCore(); <br/>");
return base.BeginExecuteCore(callback, state);
} /// <summary>
/// 异步执行
/// </summary>
/// <param name="asyncResult"></param>
protected override void EndExecuteCore(IAsyncResult asyncResult)
{
Response.Write("EndExecuteCore(); <br/>");
base.EndExecuteCore(asyncResult);
} public ActionResult Index()
{
return Content("Index();<br/>");
}
}

Controller 还继承了一个IAsyncController 接口,这个异步接口并不是指 Controller 是否异步, 而是 Action 方法是否异步执行,在MVC4以前 Action 的异步是通过

定义XxxAsync/XxxCompleted 形式定义异步Action,新版本中为了兼容旧版 所以引用了IAsyncController

在4.0后 Action 通过返回Task的方式来定义是否异步执行Action。

ASP.NET mvc4 Controllder 同步还是异步的更多相关文章

  1. ASP.NET sync over async(异步中同步,什么鬼?)

    async/await 是我们在 ASP.NET 应用程序中,写异步代码最常用的两个关键字,使用它俩,我们不需要考虑太多背后的东西,比如异步的原理等等,如果你的 ASP.NET 应用程序是异步到底的, ...

  2. 异步多线程 ASP.NET 同步调用异步 使用Result产生死锁

    一个方法调用了async方法,要将这个方法本身设计为async. public class BlogController : Controller { public async Task<Act ...

  3. C# ASP.NET Core使用HttpClient的同步和异步请求

    引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...

  4. ASP.NET WebAPi(selfhost)之文件同步或异步上传

    前言 前面我们讲过利用AngularJs上传到WebAPi中进行处理,同时我们在MVC系列中讲过文件上传,本文结合MVC+WebAPi来进行文件的同步或者异步上传,顺便回顾下css和js,MVC作为客 ...

  5. Asp.Net MVC4新特性指南(2):新特性介绍

       上一章讲解了最基本的MVC4说明.今天就介绍下几种新特性的使用例子:   就当大家有MVC3的基础了.在这个基础上在看下面的介绍就容易多了.1.Web API MVC4包括一个更好的解决方案:A ...

  6. Asp.Net MVC4 + Oracle + EasyUI 学习 第二章

    Asp.Net MVC4 + Oracle + EasyUI 第二章 --使用Ajax提升网站性能 本文链接:http://www.cnblogs.com/likeli/p/4236723.html ...

  7. Asp.Net MVC4 + Oracle + EasyUI 学习 序章

    Asp.Net MVC4 + Oracle + EasyUI  序章 -- 新建微软实例 本文链接:http://www.cnblogs.com/likeli/p/4233387.html 1.  简 ...

  8. ASP.NET MVC EF 中使用异步控制器

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   为什么使用异步操作/线程池 ASP.NET MVC ...

  9. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序使用异步及存储过程

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第九篇:为ASP.NET MVC应用程序 ...

随机推荐

  1. 版本控制git之二-分支

    有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 为何 Git 的分支模型如此出众呢? Git 处理分支的方式可谓是难以置信的轻量, ...

  2. [ExtJs6] 环境搭建及创建项目

    1. 环境搭建 sencha cmd 和 extjs6 sdk. sencha cmd: https://www.sencha.com/products/extjs/cmd-download/ ext ...

  3. 页面生成柱状图 --- D3.js

    转载自:https://www.cnblogs.com/fastmover/p/7779660.html D3.js从入门到"放弃"指南 前言 近期略有点诸事不顺,趁略有闲余之时, ...

  4. 通过CSS 给界面必选项添加星号

    在制作网页的时候,如果一个选项是必填的,通常会给选项添加一个红色星号,来引起用户的注意:最近笔者刚好遇见一个类似的需求,本来可以用html标签和style就可搞定,由于笔者需要改动的界面比较多(六个) ...

  5. Struts2结果页面配置(Result)

    1.全局页面配置 全局结果页面:针对一个包下的所有Action的页面跳转都可生效 <global-results> <result name="xxx">/ ...

  6. beego 自定义模板函数

    beego支持的模板函数不是很多,有时候前端展现数据的时候,要对数据进行格式化,所以要用到自定义模板函数 比如我的前端模板上有时间和模板大小这2个数据,原始数据都是int的时间戳和byte单位的数据, ...

  7. Linux实战教学笔记31:Keepalived高可用集群应用实践

    1.1 Keepalived高可用软件 1.1.1 Keepalived介绍 Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入 ...

  8. sax解析xml文件,封装到对象中

    创建User.java类 public class User { private String id; private String name; private String age; private ...

  9. 论immutable不可变性

    什么叫immutable和mutable?简单来讲,一个immutable的对象一旦被创建好,它的状态将不会改变.反过来,如果一个类的实例是immutable的,那么我们把这个类也称作immutabl ...

  10. validator 参数校验的常用注解

    @AssertFalse Boolean,boolean 验证注解的元素值是false @AssertTrue Boolean,boolean 验证注解的元素值是true @NotNull 任意类型 ...