如何在c#web api中创建标题/正文验证过滤器

时间:2014-03-25 05:40:34

标签: c#-4.0 asp.net-web-api webapp2 wcf-web-api

我用post方法传递了请求。我想使用过滤器验证请求(标题/正文)。我如何使用Web api配置东西。  以下是我的要求:

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中调用过滤器,任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

验证可以通过两种方式广泛执行1.使用过滤器2.全局处理程序 您可以在下面查看过滤器的链接 http://www.dotnetcurry.com/showarticle.aspx?ID=927

  1. 使用Global hanlders
  2. 添加新课程并使用以下代码

    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/

    我希望这会对你有所帮助