C# 如何在IActionFilter中获取类型化响应对象。ASP核心2.2
在控制器操作中,我返回类型化的c#对象: 一些答案建议使用C# 如何在IActionFilter中获取类型化响应对象。ASP核心2.2,c#,asp.net-core,C#,Asp.net Core,在控制器操作中,我返回类型化的c#对象: 一些答案建议使用actionExecutedContext.Response.TryGetContentValue,但我的context.HttpContext.Response中没有这个选项。 也没有内容对象: 提到了类型为的Resonse属性 因此,您可以通过以下方式访问HttpContent context.Response.Content 例如: var content = context.Response.Content; if (c
actionExecutedContext.Response.TryGetContentValue
,但我的context.HttpContext.Response中没有这个选项。
也没有内容对象:
提到了类型为的Resonse属性
因此,您可以通过以下方式访问HttpContent
context.Response.Content
例如:
var content = context.Response.Content;
if (content != null) {
var value = content.ReadAsStringAsync().Result; //holding the returned value
}
或者试试看
var content = context.Response.Content as ObjectContent;
if (content != null) {
var value = content.value; //holding the returned value
}
提到类型为的Resonse属性
因此,您可以通过以下方式访问HttpContent
context.Response.Content
例如:
var content = context.Response.Content;
if (content != null) {
var value = content.ReadAsStringAsync().Result; //holding the returned value
}
或者试试看
var content = context.Response.Content as ObjectContent;
if (content != null) {
var value = content.value; //holding the returned value
}
使用以下命令,以获得结果
context.Result
使用以下命令,以获得结果
context.Result
如何在IActionFilter中获取DTO.CartResponse
public void OnActionExecuted(ActionExecutedContext context)
{
DTO.CartResponse response = ?
}
请参阅以下代码段:
public void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result.GetType().Equals(typeof(Microsoft.AspNetCore.Mvc.ObjectResult)))
{
var res = ((Microsoft.AspNetCore.Mvc.ObjectResult)context.Result).Value;
if (res.GetType().Equals(typeof(CartResponse)))
{
CartResponse response = res as CartResponse;
}
}
}
如何在IActionFilter中获取DTO.CartResponse
public void OnActionExecuted(ActionExecutedContext context)
{
DTO.CartResponse response = ?
}
请参阅以下代码段:
public void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result.GetType().Equals(typeof(Microsoft.AspNetCore.Mvc.ObjectResult)))
{
var res = ((Microsoft.AspNetCore.Mvc.ObjectResult)context.Result).Value;
if (res.GetType().Equals(typeof(CartResponse)))
{
CartResponse response = res as CartResponse;
}
}
}
也许可以通过使用像wireshark或fiddler这样的嗅探器来了解实际返回的内容。每个服务器将返回不同的响应。响应将取决于请求中的属性(标题)和数据库中的数据。@jdweng我的问题是关于在asp core 2.2筛选器中获取响应对象。ASP核心过滤器:MSDN示例只是示例代码,响应将因您使用的服务器而异。您必须根据收到的响应修改示例代码。可以使用wireshark或fiddler等嗅探器进行学习,以查看实际返回的内容。每个服务器将返回不同的响应。响应将取决于请求中的属性(标题)和数据库中的数据。@jdweng我的问题是关于在asp core 2.2筛选器中获取响应对象。ASP核心过滤器:MSDN示例只是示例代码,响应将因您使用的服务器而异。您必须根据收到的响应修改示例代码。我没有context.response.Content。我有context.HttpContext.Response,它没有ReadAsStringAsync()方法。请尝试第二个选项。我在什么地方见过,但我不确定它是否有效。让我知道请同时让我知道内容对象的类型。在调试过程中,通过将鼠标悬停在它上让我知道,以便我可以为您提供更多帮助。我没有context.Response.Content。我有context.HttpContext.Response,它没有ReadAsStringAsync()方法。请尝试第二个选项。我在什么地方见过,但我不确定它是否有效。让我知道请同时让我知道内容对象的类型。在调试过程中,将鼠标悬停在它上面,让我知道,这样我可以帮助您更多。可能是因为结果是IAction结果的类型。您可以发布完整的代码吗?可能是因为结果是IAction结果的类型。您可以发布完整的代码吗?