c# 4.0 - how to create header/body validation filters in c# webapi -
i passed request post method. want validate request(header/body) using filters. how can configure thing using web api. below request :
x-ln-request:<?xml version="1.0" encoding="utf-8" standalone="yes"?><ns2:requesttoken xmlns:ns2="http://services.lexisnexis.com/xmlschema/request-token/1"><transactionid>2886f786-bd20-4220-932b-1bca1a9f7710</transactionid><sequence>1.2.2.2</sequence><contextualfeaturepermid>1000516</contextualfeaturepermid><featurepermid></featurepermid><billbackstring descriptionpermid="">-none-</billbackstring><ismandatorybillbackenforced>false</ismandatorybillbackenforced><cpmfeaturecode>47</cpmfeaturecode></ns2:requesttoken> x-ln-session:<?xml version="1.0" encoding="utf-8"?><ns2:sessiontoken xmlns:ns2="http://services.lexisnexis.com/xmlschema/session-token/1"><sessionid>c2e9d6f8-6505-4f59-a453-0f7014e58832</sessionid><issued>2014-03-24t02:59:31.484-04:00</issued><userpermidurn>urn:user:ca148686</userpermidurn><authorizationpermid>1000202</authorizationpermid><signature>v1-ffa9cbc5d0c27c7a36e1a2698fb11189</signature></ns2:sessiontoken> x-ln-i18n: x-ln-retrieveoptions: x-ln-application: body :
<ns3:renderjob xmlns:ns2="http://services.lexisnexis.com/xmlschemas/linktemplate/1" xmlns:ns3="http://services.lexisnexis.com/shared/xmlschema/renderer/3" xmlns:ns4="http://services.lexisnexis.com/shared/xmlschema/coredataitem/2" xmlns:ns5="http://services.lexisnexis.com/shared/xmlschema/subdataitem/2" xmlns:ns6="http://services.lexisnexis.com/shared/xmlschema/clientmatter/1" xmlns:ns7="http://services.lexisnexis.com/shared/xmlschema/servicescommon/2> how call filters in webapi, can 1 me on this.
the validation can performed broadly in 2 ways 1. using filters 2. global handlers can check below link filters http://www.dotnetcurry.com/showarticle.aspx?id=927
- using global hanlders
add new class , use below code
class samplehandler : delegatinghandler { public samplehandler () { } protected override async task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken) { //todo: validation goes here httpresponsemessage response = await base.sendasync(request, cancellationtoken); if (!response.issuccessstatuscode) { //_writer.writeline("{0}\t{1}\t{2}", request.requesturi, // (int)response.statuscode, response.headers.date); } return response; } }
add line in webapiconfi file
config.messagehandlers.add(new samplehandler()); reference: http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api https://brettedotnet.wordpress.com/2013/05/01/asp-net-web-api-validation-a-one-more-better-approach/
i hope you
Comments
Post a Comment