从抽象类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. Python函数部分

    Python函数的初识 Python函数的进阶 Python中的闭包与迭代器 Python生成器/推导式/生成器表达式 Python内置函数二 (递归函数,匿名函数,二分法)

  2. Netty面试题

    1.BIO.NIO和AIO的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理.线程开销大. 伪异步IO:将请求连接放入线程池,一对多,但线程还是很宝贵的资源. N ...

  3. POI-Excel表格导入和导出

    ExcelWriter /** * @author zuzhilong * @date 2013-10-10 下午08:04:02 * @desc 生成导出Excel文件对象 * @modify * ...

  4. django -- url 的 默认值

    在urls.py里可以直接向函数传递默认值,看代码: urls.py from django.conf.urls import url from mytest import views urlpatt ...

  5. C++Primer笔记-----day06

    ================================================================day06=============================== ...

  6. MyBatis 学习记录4 MyBatis的一级缓存

    主题 分享记录一下MyBatis的一级缓存相关的学习. Demo public static void firstLevelCache() { init("mybatis-config.xm ...

  7. [iOS]UIScrollView嵌套UITableView,超出屏幕的cell点击不了问题

    最初我是用UIScrollView嵌套了一个UIView,然后UIView里面嵌套UITableView,这样cell 就会超出屏幕那一部分点击不了. 解决方法如下,UITableView拖出来,作为 ...

  8. excel解析的几种实现方法

  9. jquery中ready函数,$(function(){})与自执行函数的区别

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. 选择性导出excel表中内容

    package com.huawei.utils; import java.io.FileNotFoundException;import java.io.FileOutputStream;impor ...