问题描述
什么是检查的服务端点请求头的最佳方法?
What is the best way to inspect the Request Headers for a service endpoint?
ContactService : Service
看了这个https://github.com/ServiceStack/ServiceStack/wiki/Access-HTTP-specific-features-in-services我很好奇,以preferred的方式来获得的接口。
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
推荐答案
里面一个ServiceStack Service您可以访问<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/ServiceHost/IHtt$p$pquest.cs\">IHtt$p$pquest和<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/ServiceHost/IHtt$p$psponse.cs\">IHtt$p$psponse使用对象:
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);
}
}
该IHtt prequest是在底层ASP.NET的Htt prequest或HttpListenerRequest的包装(取决于如果你在托管ASP.NET或自托管的HttpListener)。因此,如果你在ASP.NET运行,就可以得到基本的ASP.NET的Htt prequest有:
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];
这篇关于检查与ServiceStack请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!