net core 返回值

//指定类下的子类
Type helloType = typeof(ActionResult);
List<Type> types = new List<Type>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var type in assembly.GetTypes())
{
if (helloType.IsAssignableFrom(type))
{
if (type.IsClass && !type.IsAbstract)
{
types.Add(type);
}
}
}
}
for (int i=;i<types.Count;i++)
{
sb.AppendLine(types[i].Name);
}
var ret = sb.ToString();
public FileStreamResult(Stream fileStream, string contentType);
public FileContentResult(byte[] fileContents, string contentType); var t = new ContentResult();
t.Content = "";
return t;
报错
public class AResult : ActionResult
{
// Properties
public string Content { get; set; }
public string ContentType { get; set; }
public int? StatusCode { get; set; } public override Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<AResult>>().ExecuteAsync(context, this);
}
//pivotPoint.get_X(); 改为 pivotPoint.x; }

public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //注册
services.TryAddSingleton<IActionResultExecutor<AResult>>(); }
注册后报错

public class AResult : ActionResult
{
// Properties
public string Content { get; set; }
public string ContentType { get; set; }
public int? StatusCode { get; set; }
public override Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<AResult>>().ExecuteAsync(context, this);
}
}
public class AResultExecutor : IActionResultExecutor<AResult>
{
private const string DefaultContentType = "text/plain; charset=utf-8";
private readonly IHttpResponseStreamWriterFactory _httpResponseStreamWriterFactory; public AResultExecutor(IHttpResponseStreamWriterFactory httpResponseStreamWriterFactory)
{
_httpResponseStreamWriterFactory = httpResponseStreamWriterFactory;
} public async Task ExecuteAsync(ActionContext context, AResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
} if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
var response = context.HttpContext.Response;
ResponseContentTypeHelper.ResolveContentTypeAndEncoding(
null,
response.ContentType,
DefaultContentType,
out var resolvedContentType,
out var resolvedContentTypeEncoding); response.ContentType = resolvedContentType;
var defaultContentTypeEncoding = MediaType.GetEncoding(response.ContentType); if (result.Content != null)
{ string content = result.Content; response.ContentLength = resolvedContentTypeEncoding.GetByteCount(content);
using (var textWriter = _httpResponseStreamWriterFactory.CreateWriter(response.Body, resolvedContentTypeEncoding))
{
await textWriter.WriteAsync(content);
await textWriter.FlushAsync();
}
}
}
}
services.TryAddSingleton<IActionResultExecutor<AResult>, AResultExecutor>();
详细
自定义ActionResult
https://blog.csdn.net/u014690615/article/details/85999853
net core 返回值的更多相关文章
- ASP.NET Core Mvc中空返回值的处理方式
原文地址:https://www.strathweb.com/2018/10/convert-null-valued-results-to-404-in-asp-net-core-mvc/ 作者: F ...
- ASP.NET Core Web API处理HttpResponseMessage类型返回值的问题
在将我们的 web api 从 .NET Framework 迁移至 .net core(asp.net core 1.1)之后,遇到一个问题. 之前返回值类型为 HttpResponseMessag ...
- Net Core 中间件实现修改Action的接收参数及返回值
新一个WebApi项目(Net Core 2.1) 新建InputOutputAlterMiddleware类,修改命名空间为Microsoft.AspNetCore.Builder(不修改也没关系, ...
- Asp.net Core 异常日志与API返回值处理
需求: 1.对异常进行捕获记录日志 并且修改返回值给前端 解释: ILogger4是自定义的一个日志,更改它就好 解决方案1: 使用中间件进行异常捕获并且修改其返回值 public class Err ...
- ASP.NET Core搭建多层网站架构【11-WebApi统一处理返回值、异常】
2020/02/01, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[11-WebApi统一处理返回值.异常] 使用I ...
- ASP.NET Core中的Action的返回值类型
在Asp.net Core之前所有的Action返回值都是ActionResult,Json(),File()等方法返回的都是ActionResult的子类.并且Core把MVC跟WebApi合并之后 ...
- net core webApi返回值
1 多个参数采用结构的形式,如class xyz 2 返回值使用IActionResult 控制,不能使用httpRequestMessage类型 3 url为路由名称+Controller前缀 如下 ...
- asp.net core webapi 统一处理返回值、异常和请求参数验证
现在的开发模式很少用asp.net mvc一个项目直接操作界面和数据库了.大部分都使用前后端分离,更多的是为了让API支持移动端. 后端写webapi的时候必然需要和前端约定请求值和返回值的格式,如果 ...
- Spring框架下的 “接口调用、MVC请求” 调用参数、返回值、耗时信息输出
主要拦截前端或后天的请求,打印请求方法参数.返回值.耗时.异常的日志.方便开发调试,能很快定位到问题出现在哪个方法中. 前端请求拦截,mvc的拦截器 import java.util.Date; im ...
随机推荐
- 【LeetCode】123、买卖股票的最佳时机 III
Best Time to Buy and Sell Stock III 题目等级:Hard 题目描述: Say you have an array for which the ith element ...
- 小记----采集之Xpath
一.XPATH简介 Xpath是XML路径语言,它是一种确定XML文档中某部分位置的语言 二.XPATH语法 XPATH使用路径表达式在XML文档中选取节点.节点是通过沿着路径或者step来选取 ...
- hibernate字段映射枚举类型
上一篇介绍了mybatis字段映射枚举类型,这一篇给大家介绍一下hibernate字段怎么去映射枚举类型的(这只是一种参考方式,映射方法还有很多种). 还是以上篇sku表为例,sku表里一个statu ...
- 遍历dataframe
import pandas as pd import os if __name__ == '__main__': Folder_Path = 'c:\checklog' os.chdir(Folder ...
- Luogu P1631 序列合并
题目 开一个堆,先把所有\(a[i]+b[1]\)压进优先队列. 然后每次把最小的取出来,把对应的\(a[i]\)的下一个\(b[j]\)拿出来加进去. #include<bits/stdc++ ...
- div距离左边设置
margin-right:不加负号, margin-left:必须加负号,理解为倒数 margin-left:-10px;
- laravel5.5入门-安装和认证
一.安装 在终端CMD里切换到你想要放置该网站的目录下(如 d:\project\laravel),运行命令 composer create-project laravel/laravel learn ...
- 缓存---LRU算法实现
2.LRU 以下是基于双向链表+HashMap的LRU算法实现,对算法的解释如下: 设置一个map存放对应的键和值,同时设置一个双向链表,来保存最近最久未使用的关系,如果访问一个键,键存在于m ...
- JavaSE--类与对象
一.类 类是具有相同特性(数据元素)和行为(功能)的对象的抽象就是类.因此,对象的抽象是类,类的具体化就是对象,也可以说类的实例是对象,类实际上就是一种数据类型.类具有属性,它是对象的状态的抽象,用数 ...
- Quartz.net 3.x使用总结(一)——简单使用
原文:Quartz.net 3.x使用总结(一)--简单使用 阅读目录 1.Quartz.net简介 2.简单使用 3.TriggerBuilder介绍 3.1 SimpleSchedule 3.2 ...