使用ServiceStack检查请求标头

时间:2013-04-03 00:17:35

标签: asp.net servicestack

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

ContactService : Service

阅读此https://github.com/ServiceStack/ServiceStack/wiki/Access-HTTP-specific-features-in-services后,我很想知道接触界面的首选方法。

谢谢你, 斯蒂芬

1 个答案:

答案 0 :(得分:7)

在ServiceStack Service内,您可以访问IHttpRequestIHttpResponse个对象:

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