c# - How can I consolidate all of my "setup code" away from my WebAPI controllers? -


All my controllers look like this:

  [htmlpot] [root (" / Business name ")] [Authorized] Public HTTPPPS Pacemats Update Business Business (Business Name Design Model) {If (ModelState. ISIIDID) Refund Request CreteErrorpressPage (HTTPTitcod.badrequest, modelstate); Try {_userService.UpdateBusinessName (User.Identity.Name, model.BusinessName); Return request Cresparsons (HTPTatus code. OK, new APIsPointData () {}); } Hold (exception e) {// logging code // return request. CaterSpons (HTTTPTas code; Eternal server error, E); Return request. Create response (HTTPITAS code OK, new APRSponseSports () {successful = false, error = "something bad happened: ("});}}   

one lot Repeated goods in my controllers can ideally be to me:

  [hppit] [root ("API / business name")] [authorized] public HTTPPS message message Business business (business name by name) {_userService.UpdateBusinessName (User.Identity.Name, model.BusinessName); Refund Request. ET RESPENCE (HTTPITUS CODE. OK, NEW APRIPS Points DEO () {});}   

And tell to WebAPI with all the other stuff ... but I do not know if this is possible How can I do this?

You can do the following: < p> 1) Create a verification filter so that your action can be executed only when the model state is valid. So that you do not have to check the modelstate.You are now in your actions methods.

  Public class validationAction filter: ActionFilterArrbit {Public Override Zero Onset Exposing (HTTPPConnectX Action Contex) {ModelStateExtension ModelState = Action Contex. Modestate; If (! ModelState.IsValid) {actionContext.Response = actionContext.Request.CreateErrorResponse (HTTPTitas code.badrequest, modelstate); }}}   

2) Create an exception handling filter, which will catch any exception by the verb method, serial it and make the client an HTTP BadRequest response message. So do not try to catch you anymore in your action message.

  Public class handle extension filter: Exception filter attribute {public override zero on expansion (HTPActioned contact reference) {var responseMessage = new HTTPTPSMS (HTTPTPictcode. Bad request); ResponseMessage.Content = New string content (context.Exception.Message); Reference. Feedback = feedback message; }}   

You can register these filters in WebApiConfig.cs by adding the following lines

  config.Filters.Add (new verificationAction filter ()); Config.Filters.Add (new handling extension ()); To be more specific for the SB 2055 scenario, I am adding the code below.  HandleExceptionFilter: ExceptionFilterAttribute {Public override zero on option Model = New APIspoint () {successful = false, error = reference Extension. Message}) reference. Response = Reference. request. }}    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -