一:背景

1. 讲故事

前几天群里有一位朋友聊到,为什么我在 Action 中执行一句 Response.Write 之后,后续的 View 就不呈现了,如果脑子中没有画面,那就上测试代码:


  1. public class HomeController : Controller
  2. {
  3. public IActionResult Index()
  4. {
  5. Response.WriteAsync("hello world!");
  6. return View();
  7. }
  8. }

结果还是挺有意思的,大家都知道,默认情况下会渲染 /Home/Index 对应的 view 页面,但这里被 Response.WriteAsync 插了一杠子,气的 view 都渲染不出来了,那接下来就来找一找 view 为啥这么生气?

二:寻找真相

1. 从 Logger 入手

相信很多人都在用 aspnetcore 中的 logger 记录日志,为什么要首选这个 logger 呢?因为它在 web框架 中是一等公民的存在,毕竟底层源码各处都嵌入着这玩意哈,随便找点代码:


  1. internal abstract class ActionMethodExecutor
  2. {
  3. private Task ResultNext<TFilter, TFilterAsync>(ref ResourceInvoker.State next, ref ResourceInvoker.Scope scope, [Nullable(2)] ref object state, ref bool isCompleted) where TFilter : class, IResultFilter where TFilterAsync : class, IAsyncResultFilter
  4. {
  5. ResourceInvoker.ResultExecutingContextSealed resultExecutingContext3 = this._resultExecutingContext;
  6. this._diagnosticListener.BeforeOnResultExecuting(resultExecutingContext3, tfilter);
  7. this._logger.BeforeExecutingMethodOnFilter(filterType, "OnResultExecuting", tfilter);
  8. tfilter.OnResultExecuting(resultExecutingContext3);
  9. this._diagnosticListener.AfterOnResultExecuting(resultExecutingContext3, tfilter);
  10. this._logger.AfterExecutingMethodOnFilter(filterType, "OnResultExecuting", tfilter);
  11. if (this._resultExecutingContext.Cancel)
  12. {
  13. this._logger.ResultFilterShortCircuited(tfilter);
  14. this._resultExecutedContext = new ResourceInvoker.ResultExecutedContextSealed(resultExecutingContext3, this._filters, resultExecutingContext3.Result, this._instance)
  15. {
  16. Canceled = true
  17. };
  18. goto IL_39E;
  19. }
  20. }
  21. }

而且大家想想,这种写法特别奇葩,我想底层框架中的 logger 定会有所反馈,接下来在启动程序的时候采用 WebApplication1 的模式启动,如下图:

启动后,在控制台上可以看到一堆报错信息:


  1. info: Microsoft.Hosting.Lifetime[0]
  2. Now listening on: http://localhost:5000
  3. info: Microsoft.Hosting.Lifetime[0]
  4. Application started. Press Ctrl+C to shut down.
  5. info: Microsoft.Hosting.Lifetime[0]
  6. Hosting environment: Development
  7. info: Microsoft.Hosting.Lifetime[0]
  8. Content root path: E:\net5\WebApplication1\WebApplication1
  9. fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
  10. An unhandled exception has occurred while executing the request.
  11. System.InvalidOperationException: Headers are read-only, response has already started.
  12. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowHeadersReadOnlyException()
  13. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)
  14. at Microsoft.AspNetCore.Http.DefaultHttpResponse.set_ContentType(String value)
  15. at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
  16. at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
  17. at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
  18. at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
  19. at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|21_0(ResourceInvoker invoker, IActionResult result)
  20. at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
  21. at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
  22. at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
  23. at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()

异常信息非常明显:Headers are read-only, response has already started,大概就是说,header是只读的,response已是启动状态了,从调用堆栈的 ViewExecutor.ExecuteAsync 处可看出,代码准备渲染 view,在 set_ContentType 处遭遇异常,结束了后续渲染流程。

接下来一起看下,为什么会触发这个异常???

三: 调试源码寻找异常的原因

1. dnspy 调试

除了从异常堆栈中找到最早的异常代码处,这里还说一个小技巧,使用 ndspy 的 异常断点功能,在异常设置面板 定位 InvalidOperationException 异常即可。

接下来就可以让程序跑起来,当异常抛出时会自动断下来。

仔细看一下图中的文字标注,还是很好理解的,接下来继续追一下: response.ContentType = contentType2; 内部都做了什么。


  1. public override string ContentType
  2. {
  3. get
  4. {
  5. return this.Headers[HeaderNames.ContentType];
  6. }
  7. set
  8. {
  9. if (string.IsNullOrEmpty(value))
  10. {
  11. this.HttpResponseFeature.Headers.Remove(HeaderNames.ContentType);
  12. return;
  13. }
  14. this.HttpResponseFeature.Headers[HeaderNames.ContentType] = value;
  15. }
  16. }

可以看到 内部是给 this.HttpResponseFeature.Headers 赋值的,继续往下追:

从图中可以看到,最后的 HttpHeader._isReadOnly =true 导致异常的发生,罪魁祸首哈,接下来研究下这句 HttpHeader._isReadOnly=true 是何时被赋值的。

2. _isReadOnly=true 何时发生

这个问题就简单多了,必定是 Response.WriteAsync("hello world!"); 造成了 _isReadOnly=true ,在 HttpHeader 下有一个 SetReadOnly 方法用于对 _isReadOnly 字段的封装,代码如下:


  1. internal abstract class HttpHeaders
  2. {
  3. public void SetReadOnly()
  4. {
  5. this._isReadOnly = true;
  6. }
  7. }

接下来在该方法处下一个断点,继续调试,如下图:

从图中可看到,原来 Response.WriteAsync("hello world!") 是可以封锁 HttpHeaders的,后续任何再对 HttpHeader 的操作都是无效的。。。

其实大家也可以想一想,不同的response,肯定会有不同的 header,要想叠加的话这辈子都不可能的,只能让后面的报错,如下:


  1. 1. response:
  2. HTTP/1.1 200 OK
  3. Date: Mon, 19 Oct 2020 14:37:54 GMT
  4. Server: Kestrel
  5. Transfer-Encoding: chunked
  6. c
  7. hello world!
  8. 2. view:
  9. HTTP/1.1 200 OK
  10. Date: Mon, 19 Oct 2020 14:39:01 GMT
  11. Content-Type: text/html; charset=utf-8
  12. Server: Kestrel
  13. Content-Length: 2239

四: 总结

这篇就是对群聊天过程中抛出问题的个人探究,一家之言,不过挺有意思,大家也可以多用用调试工具寻找问题,证明问题,纸上得来终觉浅,绝知此事要躬行,好了,希望本篇对您有帮助!

更多高质量干货:参见我的 GitHub: dotnetfly

为啥 Response.Write 后,View就不渲染了?的更多相关文章

  1. vue修改对象的属性值后页面不重新渲染

    原文地址:vue修改对象的属性值后页面不重新渲染 最近项目在使用vue,遇到几次修改了对象的属性后,页面并不重新渲染,场景如下: HTML页面如下: [html] view plain copy &l ...

  2. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  3. SAP MM 明明有需求,为啥MRP RUN后没有PR单据产生?

    SAP MM 明明有需求,为啥MRP RUN后没有PR单据产生? 用户报了一个问题说,对于物料号42011222的采购单 4500000156建好了,为啥PR没有自动生成 . 我们检查了物料的MRP ...

  4. 关于QT Graphics View开启OpenGL渲染后复选框、微调框等无法正常显示的问题

    之前学习QT Graphics View框架,除了基本的图元外,还可以通过QGraphicsProxyWidget类添加QT的基本Widget(如按钮.复选框.单选框等),常使用的场景类接口如下: Q ...

  5. Android 自定义可拖拽View,界面渲染刷新后不会自动回到起始位置

    以自定义ImageView为例: /** * 可拖拽ImageView * Created by admin on 2017/2/21. */ public class FloatingImageVi ...

  6. 手势识别官方教程(4)在挑划或拖动手势后view的滚动用ScrollView和 HorizontalScrollView,自定义用Scroller或OverScroller

    简单滚动用ScrollView和 HorizontalScrollView就够.自定义view时可能要自定义滚动效果,可以使用 Scroller或 OverScroller Animating a S ...

  7. iOS 动画结束后 view的位置 待完善

    默认的动画属性,动画结束后,view会回到原始位置.但是如果设定了 CAAnimation的 removedOnCompletion 属性,那么view会保持这个位置!但是真实的接收 点击的frame ...

  8. 解决TableView / ScrollView上的Menu问题(1滑出View区域还可点击2导致点击menu后View不能滑动)

    解决TableView / ScrollView上的Menu问题 1划出区域还可点击 重写CCMenu的触摸事件函数 TouchBegin/TouchMove/TouchCancle/TouchEnd ...

  9. Android-一只手指滑动View,另一只手指按Home键,重新进入后View状态无法更新的问题

    上午测试报了一个bug:说是一只手指拖动虚拟摇杆上的View滑块不松,另一只手指点击Home键将App压后台,重新进入的时候,View滑块卡死了. 刚开始看到这个问题感觉很奇怪,因为正常情况下,手指离 ...

随机推荐

  1. matlab中的多项式计算

    在做多项式加法的时候需要做多项式扩展.这里将g1扩展到与f等长 多项式的乘积,是两个多项式之和减1, 多项式求导函数:ployder() 先建立两个多项式,再求a的导函数 在计算两个多项式乘积的导函数 ...

  2. elasticsearch 索引清理脚本及常用命令

    elastic索引日志清理不及时,很容易产生磁盘紧张,官网给出curl -k -XDELETE可以清理不需要的索引日志. 清理脚本 #!/bin/bash #Author: 648403020@qq. ...

  3. 数据库:drop、truncate、delete的区别

    近日在删除数据时,发现除了常用的Delete & Drop语句之外,还有Truncate也是与删除数据相关的,针对上述三种有进行简单的比较与整理 用法 drop 用法:drop table 表 ...

  4. spring boot 源码之SpringApplication

    run方法 run方法主要创建和初始化ConfigurableApplicationContext,在各个节点调用SpringApplicationRunListener的回调函数,在发送异常时调用用 ...

  5. SpringBoot框架:通过AOP和自定义注解完成druid连接池的动态数据源切换(三)

    一.引入依赖 引入数据库连接池的依赖--druid和面向切面编程的依赖--aop,如下所示: <!-- druid --> <dependency> <groupId&g ...

  6. Flutter学习五之网络请求和轮播图的实现

    上期讲到了,怎样实现一个下拉刷新和加载更多的列表,数据更新,需要使用到网络请求,在flutter中,怎样实现一个网络请求呢?官方使用的是dart io中的HttpClient发起的请求,但HttpCl ...

  7. Swiper 在IE9 及其他浏览器使用

    Swiper 在IE9 及其他浏览器使用 前言 昨天遇到一个问题,swiper 使用版本是3.4.2 除了Ie9浏览器外其他浏览器都正常,IE9 无法轮播,执行控制台报错源码问题.没办法,只能降级兼容 ...

  8. Java源码赏析(三)初识 String 类

    由于String类比较复杂,现在采用多篇幅来讲述 这一期主要从String使用的关键字,实现的接口,属性以及覆盖的方法入手.省略了大部分的字符串操作,比如split().trim().replace( ...

  9. Android开发中导入第三方库所遇问题记录

    1.重复循环依赖的问题 (1)需求 如下图所示: 在Android 项目中,采用模块化开发,一个是主跑application--Mudule A,另外一个是library--Library B 1)M ...

  10. Laver 文件版本遍历器

    系统简介 最近有个需求,需要罗列出各个目录中文件的信息,检索各类文件的最新版本.网上看了很多方式,但发现没有合适的.于是利用空余时间开始编写了一套文件遍历系统,如此便有了Laver(紫菜).Laver ...