2014-03-25 128 views
0

我通过post方法传递了请求。我想验证使用过滤器的请求(标​​题/正文)。我如何使用Web API配置这个东西。下面 是我的要求:如何在c#webapi中创建头文件/正文验证过滤器

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: 

身体是:

<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> 

如何调用的WebAPI的过滤器,可以在任何一个帮助我在此。

回答

0

验证可以通过两种方式1.使用过滤器2.全球处理器广泛进行 您可以检查此以下链接过滤器 http://www.dotnetcurry.com/showarticle.aspx?ID=927

  1. 使用全局hanlders

添加新并使用下面的代码

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; 
    } 

}

在WebApiConfi文件中添加此行

config.MessageHandlers.Add(new SampleHandler()); 

参考: 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/

我希望这会帮助你