proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
677 stars 82 forks source link

Fluent Model Validation result #51

Closed sajidali2444 closed 4 years ago

sajidali2444 commented 4 years ago

I am trying to implement a generic model validation filter attribute here is code sample using AutoWrapper.Wrappers; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using XERide.Core;

namespace XERide.API.Filters { public class ModelValidationFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.ModelState.IsValid) { if (filterContext.HttpContext.Request.Method == "GET") { var result = new BadRequestResult(); filterContext.Result = result; } else { //throw new ApiProblemDetailsException(filterContext.ModelState); var result = new ContentResult(); string content = Helpers.JsonSerialize(new ApiError(filterContext.ModelState)); result.Content = content; result.ContentType = "application/json"; filterContext.HttpContext.Response.StatusCode = 400; filterContext.Result = result; } } } public void OnActionExecuted(ActionExecutedContext filterContext) { } } } if use ApiProblemDetailsException in filter it is not working. even in controller without ApiProblemDetailsException this is the response which is valid response I am expecting. I am using .net core 3.1 and with AutoWrapper 4 version. could please help me how to use that generic model filter validation

image

proudmonkey commented 4 years ago

Why you are implementing your own ActionFilter when you want to use AutoWrapper? AutoWrapper already takes care of that for you and all you need to do is call the ApiProblemDetailsException object directly in your Controller like in the following:

if (!ModelState.IsValid)
{
    throw new ApiProblemDetailsException(ModelState);
}
sajidali2444 commented 4 years ago

you are right, but there are numerous controllers and methods where I am posting data. that why I need it as a global filter, just write one time and automatically call in every place. and when I use throw new ApiProblemDetailsException(ModelState) then this is the error [image: image.png]

here is a code sample

[HttpPost("RegisterRider")] [AllowAnonymous] public async Task RegisterRider(RegisterRiderViewModel model) { if (!ModelState.IsValid) { throw new ApiProblemDetailsException(ModelState); }

On Mon, May 18, 2020 at 6:18 PM Vincent Maverick Durano < notifications@github.com> wrote:

Why you are implementing your own ActionFilter when you want to use AutoWrapper? AutoWrapper already takes care of that for you and all you need to do is call the ApiProblemDetailsException object directly in your Controller like in the following:

if (!ModelState.IsValid) { throw new ApiProblemDetailsException(ModelState); }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/proudmonkey/AutoWrapper/issues/51#issuecomment-630176727, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHKQ3BAEKMDXAASEJCYITDRSEYRLANCNFSM4NDZDIAA .

-- Best Regard: Sajid Ali Software Engineer (+92) 333 6528504 / 321 786 5510

sajidali2444 commented 4 years ago

if I use this if (!ModelState.IsValid) { throw new ApiProblemDetailsException(ModelState); } this is the response. image

any suggestion???

sajidali2444 commented 4 years ago

@proudmonkey could you please respond above question.

sajidali2444 commented 4 years ago

@proudmonkey I have resolved the above-mentioned issues. one thing weird for me in case of success response I am getting IsError/Is and in case of error I am getting isError it is not valid it should be same in both cases, either IsError or isError is there any config issue.

app.UseApiResponseAndExceptionWrapper(new AutoWrapperOptions { IsApiOnly = false, UseCamelCaseNamingStrategy = false, ShowStatusCode = true, UseApiProblemDetailsException = true, IsDebug = true });

proudmonkey commented 4 years ago

The default setup for AutoWrapper is camelCase (e.g isError). Perhaps there's something in your code that overrides your json configuration settings.

proudmonkey commented 4 years ago

@sajidali2444 what will happen if you removed this line:

UseCamelCaseNamingStrategy = false,