Examine Request Headers with ServiceStack

让人想犯罪 __ 提交于 2019-12-04 22:17:40

问题


What is the best way to inspect the Request Headers for a service endpoint?

ContactService : Service

Having read this https://github.com/ServiceStack/ServiceStack/wiki/Access-HTTP-specific-features-in-services I'm curious as to the preferred way to get to the Interface.

Thank you, Stephen


回答1:


Inside a ServiceStack Service you can access the IHttpRequest and IHttpResponse objects with:

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

The IHttpRequest is a wrapper over the underlying ASP.NET HttpRequest or HttpListenerRequest (depending if you're hosting on ASP.NET or self-hosted HttpListener). So if you're running in ASP.NET you can get the underlying ASP.NET HttpRequest with:

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


来源:https://stackoverflow.com/questions/15776800/examine-request-headers-with-servicestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!