c# - Trying to throw a BadRequest to my ExceptionFilterAttribute -
currently log our errors using custom attribute filter this:
public class logexceptionfilterattribute : exceptionfilterattribute { // private properties private readonly logprovider _logger; /// <summary> /// our default constructor /// </summary> /// <param name="logger"></param> public logexceptionfilterattribute(logprovider logger) { this._logger = logger; } /// <summary> /// invoked when exception has been thrown /// </summary> /// <param name="context">the context</param> public override async void onexception(httpactionexecutedcontext context) { // our user var requestcontext = context.request.getrequestcontext(); var user = requestcontext.principal.identity; // create our response var message = await _logger.error(context.exception, user); var content = new httpresponsemessage { content = new stringcontent(message), statuscode = getstatuscodefromexception(context) }; // assign our response our context context.response = content; } /// <summary> /// gets status code error /// </summary> /// <param name="context">the context</param> /// <returns></returns> private static httpstatuscode getstatuscodefromexception(httpactionexecutedcontext context) { // cast exception httpexception var exception = context.exception httpexception; // if don't have exception if (exception != null) { // return httpexception code return (httpstatuscode)exception.gethttpcode(); } // return internal server error return httpstatuscode.internalservererror; } }
i want throw bad request inside code (not in controller), tried :
throw new httpexception(400, "model not valid because: " + string.join(environment.newline, results.select(s => s.errormessage).toarray()));
i hoping pick correct statuscode instead returns 500. know how can modify code gets correct 400 code?
Comments
Post a Comment