Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Action Filters returns an empty body in asp.net-core 2.0 #7214

Closed
jernejk opened this issue Jan 9, 2018 · 7 comments
Closed

Action Filters returns an empty body in asp.net-core 2.0 #7214

jernejk opened this issue Jan 9, 2018 · 7 comments
Assignees

Comments

@jernejk
Copy link

jernejk commented Jan 9, 2018

I have same problem as #5594 and the potential fix doesn't work.
When Result is set, status code is changed (422) but response is empty.

I tried different combinations of setting up the actionExecutedContext.Exception and actionExecutedContext.ExceptionHandled.

Initial code:

public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
{
    if (actionExecutedContext.Exception is ModelValidationException validationException)
    {
        var result = new BadRequestObjectResult(validationException.Errors) { StatusCode = 422 };
        actionExecutedContext.Exception = null;
        actionExecutedContext.ExceptionHandled = true;
        actionExecutedContext.Result = result;
    }

    base.OnActionExecuted(actionExecutedContext);
}

As well as suggested workaround:

public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
{
    if (actionExecutedContext.Exception is ModelValidationException validationException)
    {
        actionExecutedContext.ExceptionHandled = false; // mark exception as handled
        actionExecutedContext.HttpContext.Response.Clear();
        actionExecutedContext.HttpContext.Response.StatusCode = 422;
        actionExecutedContext.HttpContext.Response.ContentType = new MediaTypeHeaderValue("application/json").ToString();
        string json = JsonConvert.SerializeObject(validationException.Errors);
        actionExecutedContext.HttpContext.Response.WriteAsync(json, Encoding.UTF8).Wait();
    }

    base.OnActionExecuted(actionExecutedContext);
}

It works as expected on OnActionExecuting:

public override void OnActionExecuting(ActionExecutingContext actionContext)
{
    if (!actionContext.ModelState.IsValid)
    {
        //422 - Unprocessable Entity
        var result = new BadRequestObjectResult(actionContext.ModelState) { StatusCode = 422 };
        actionContext.Result = result;
    }
}

Edit:
The ValidationException.Errors is Dictionary<string, IEnumerable<string>> so that JSON structure is consistent with actionContext.ModelState.

@rathorer
Copy link

I changed actionExecutedContext.ExceptionHandled = true; to
actionExecutedContext.ExceptionHandled = false;.
It worked for me.

@mkArtakMSFT
Copy link
Member

@rynowak, @dougbu what is the expected behavior in the first sample @rathorer has shared?

@rynowak rynowak changed the title Exception Filters returns an empty body in asp.net-core 2.0 Action Filters returns an empty body in asp.net-core 2.0 Jan 10, 2018
@rynowak
Copy link
Member

rynowak commented Jan 10, 2018

Wait, never mind, this is an action filter not an exception filter.

@rynowak
Copy link
Member

rynowak commented Jan 10, 2018

It's expected that setting ActionExecutedContext.Result to a result value will replace the action result returned by the method and cause the new action result to run if the action is considered successful.
Setting:

actionExecutedContext.Exception = null;

OR

actionExecutedContext.ExceptionHandled = true;

Should mark the action as successful.

Do you have a repro project you can share for this?

@jernejk
Copy link
Author

jernejk commented Jan 10, 2018

@rathorer solution worked!
It seems I haven't used actionExecutedContext.ExceptionHandled = false; for all of the combinations.

@rynowak I'll create a repro where having actionExecutedContext.ExceptionHandled = true; didn't work properly on the weekend.

Thanks for the help, we can now do proper validation for the forms. :)

@mkArtakMSFT
Copy link
Member

@jernejk, have you got a chance to prepare a sample repro project so we can investigate it?

@mkArtakMSFT
Copy link
Member

Hi. We're closing this issue as no response or updates have been provided in a timely manner and we have been unable to reproduce it. If you have more details and are encountering this issue please add a new reply and re-open the issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants