检查与ServiceStack请求头(Examine Request Headers with Se

2019-08-19 04:27发布

什么是来检查服务端点请求头的最佳方式?

ContactService : Service

读到这里https://github.com/ServiceStack/ServiceStack/wiki/Access-HTTP-specific-features-in-services我很好奇,以最佳的方式来获得的接口。

谢谢你,斯蒂芬·

Answer 1:

里面ServiceStack 服务您可以访问IHttpRequest和IHttpResponse使用对象:

public class ContactService : Service 
{
    public object Get(Contact request)
    {
        var headerValue = base.Request.Headers[headerKey];

        //or the same thing via a more abstract (and easier to Mock):
        var headerValue = base.RequestContext.GetHeader(headerKey);
    }
}

该IHttpRequest是在底层ASP.NET的HttpRequest或HttpListenerRequest的包装(视您主办ASP.NET或自托管HttpListener)。 所以,如果你在ASP.NET运行,就可以得到下面的ASP.NET的HttpRequest有:

var aspnetRequest = (HttpRequest)base.Request.OriginalRequest;
var headerValue = aspnetRequest.Headers[headerKey]; 


文章来源: Examine Request Headers with ServiceStack