在我的控制器完成OnActionExecuted的操作后,我正在尝试做一些事情.但是该方法被调用两次.
我的过滤方法
public class TestFilter: ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
//do stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
和我的控制器
[TestFilter]
public class BaseController : ApiController
{
public LoginResponseDTO Login(LoginRequestDTO loginRequestDTO)
{
//do login stuff
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个过滤器时,onActionExecuted方法被调用两次,这导致我在方法中的操作被应用两次到响应.我搜索了一个原因但找不到解决方案.
有任何想法吗?
asp.net-mvc onactionexecuted actionfilterattribute asp.net-web-api
我有一个ActionFilter,在OnActionExecuted方法上有一个覆盖.在POST操作中,filterContext.Controller.ViewData.Model始终为null.我确实发现下面的文章似乎说它不应该是null但是这肯定是MVC的早期版本.这是MVC3.我该怎么办?
更新:
我已经找到了原始问题的答案.我有一个自定义ActionResult,它使用自定义日期格式化程序输出JSON.问题是模型没有在控制器中设置.
在我的自定义ActionResult中,ExecuteResult方法传递了ControllerContext,如果我可以在那里设置模型那将是很好的:
context.Controller.ViewData.Model = _data;
Run Code Online (Sandbox Code Playgroud)
但这是在循环的后期,结果在ActionFilter中仍为null.这似乎意味着我需要在控制器中手动设置模型:
ControllerContext.Controller.ViewData.Model = model;
Run Code Online (Sandbox Code Playgroud)
要么
View(model);
Run Code Online (Sandbox Code Playgroud)
这意味着我每次使用这个自定义ActionResult时都需要记住这样做.有更优雅的方式吗?
再来一次更新:
我找到了一种方法来做到这一点它并不像我希望的那样优雅.
在我的comstom ActionResult的构造函数中,我在控制器中发送,至少它始终是一致的:
public JsonNetResult(object data, Controller controller) {
SerializerSettings = new JsonSerializerSettings();
_data = data;
controller.ControllerContext.Controller.ViewData.Model = _data;
}
Run Code Online (Sandbox Code Playgroud) 我制作了一个ActionFilterAttribute,它实现了OnActionExecuted方法.这意味着,它在Action方法之后运行.但是,在某些情况下,我希望OnActionExecuted不被执行.
从Action方法中,我如何阻止ActionFilter被执行?
现在,我已经做到了:
关于Action方法:
RouteData.Values.Add("CancelActionFilter", true);
Run Code Online (Sandbox Code Playgroud)
并在ActionFilter.OnActionExecuted()上:
if (filterContext.RouteData.Values["CancelActionFilter"] != null)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
但我认为这可能存在一种更优雅的方法.
我正在写custom filter我的内容Web API,我想修改方法ActionExecutedContext内部的结果OnActionExecuted。
我得到的结果类型与返回的OkObjectResult一样。action methodIActionResult
public void OnActionExecuted(ActionExecutedContext context)
{
var myResult = context.Result;
//Type of myResult is OkObjectResult
}
Run Code Online (Sandbox Code Playgroud)
那么这里我如何将其转换OkObjectResult为我的模型Object,以便我可以使用properties并操作这些值。
感谢任何建议。
custom-action-filter onactionexecuted asp.net-core-webapi asp.net-core-2.1
如标题,是否可以获取请求参数?
我尝试从请求正文中获取它,但失败了,task.Result为零。看来请求正文是空的。
任何人都可以帮助我,我将不胜感激。
代码如下:
public class LogFilter : Attribute, IActionFilter
{
private ILogger<LogFilter> _logger;
private ITestAService _service;
public LogFilter(ILogger<LogFilter> logger, ITestAService service)
{
_logger = logger;
_service = service;
}
public void OnActionExecuted(ActionExecutedContext context)
{
var content = new StringBuilder();
using (Stream sm = context.HttpContext.Request.Body)
{
int count = 0;
byte[] buffer = new byte[1024];
StringBuilder builder = new StringBuilder();
var task = sm.ReadAsync(buffer, 0, 1024);
if (task.Result > 0)
{
content.Append(Encoding.UTF8.GetString(buffer, 0, count));
}
}
}
}
Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×2
.net ×1
.net-core ×1
c# ×1
controller ×1
model ×1
null ×1
parameters ×1
request ×1