Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core and ActionFilter

I move old MVC 5 application to Core, old application has code:

public class ValidateApiModelStateAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (!actionContext.ModelState.IsValid)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            foreach (var key in actionContext.ModelState.Keys)
            {
                result.Add(key, String.Join(", ", actionContext.ModelState[key].Errors.Select(p => p.ErrorMessage)));
            }
            // 422 Unprocessable Entity Explained
            actionContext.Response = actionContext.Request.CreateResponse<Dictionary<string, string>>((HttpStatusCode)422, result);
        }
    }
}

so, it means, that if model state is not valid, then we return dictionary with errors and 422 status code (client's requirements).

I try to rewrite it the following way:

[ProducesResponseType(422)]
public class ValidateApiModelStateAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (!context.ModelState.IsValid)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            foreach (var key in context.ModelState.Keys)
            {
                result.Add(key, String.Join(", ", context.ModelState[key].Errors.Select(p => p.ErrorMessage)));
            }

            // 422 Unprocessable Entity Explained
            context.Result = new ActionResult<Dictionary<string, string>>(result);
        }
    }
}

but it can't be compiled:

Cannot implicitly convert type Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.Dictionary<string, string>> to Microsoft.AspNetCore.Mvc.IActionResult

how to do it?

like image 461
Oleg Sh Avatar asked Dec 01 '18 18:12

Oleg Sh


People also ask

Is ASP.NET Core and .NET Core different?

NET Core is a runtime to execute applications build on it. ASP.NET Core is a web framework to build web apps, IoT apps, and mobile backends on the top of . NET Core or . NET Framework.

What is Actionfilter?

Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns. Result filters contain logic that is executed before and after a view result is executed.

Is ASP.NET MVC a NET Core?

The main difference between ASP.NET Core and ASP.NET MVC 5 is their cross-platform approach. ASP.NET Core can be used on Windows, Mac, or Linux, whereas ASP.NET MVC 5 can only be used for applications on Windows. The ASP.NET Core MVC is a framework for building web apps and APIs, optimized for use with ASP.NET Core.


1 Answers

Contrary to belief ActionResult<TValue> is not derived from IActionResult. Thus the error.

Return new ObjectResult and set status code as desired.

[ProducesResponseType(422)]
public class ValidateApiModelStateAttribute : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext context) {
        if (!context.ModelState.IsValid) {
            var result = new Dictionary<string, string>();
            foreach (var key in context.ModelState.Keys) {
                result.Add(key, String.Join(", ", context.ModelState[key].Errors.Select(p => p.ErrorMessage)));
            }

            // 422 Unprocessable Entity Explained
            context.Result = new ObjectResult(result) { StatusCode = 422 };
        }
    }
}
like image 113
Nkosi Avatar answered Sep 30 '22 18:09

Nkosi