在WCF REST服务中获取原始请求URL [英] Get original request url in WCF REST service
问题描述
我必须在WCF Rest Web服务中检索原始请求URL.现在我的代码如下:
I've to retrieve the orginal request url in my WCF rest webservice. Now my code looks like this:
public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
base.CheckAccessCore(operationContext);
var url = operationContext.IncomingMessageProperties.Via.OriginalString;
...
web.config
web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
如果我的原始网址是
http://192.168.1.100:8081/test
http://192.168.1.100:8081/test
此代码返回
http://hostname:8081/test
有没有办法检索确切的原始请求网址?
is there a way to retrieve the exact original request url?
注意
我在web.config中找到了一些关于将"baseAddress"标签进行剪切的帖子,但是我的扩展名Web服务没有特定的终结点,因此我不想添加它.我不知道是否有没有端点的方法.
I found posts talking about cutomize "baseAddress" tag in web.config but I've no specific endpoint fom my extensionles webservice and I don't want to add it. I don't know if there is a way to do it without endpoint.
我找到了这个帖子 https://stackoverflow.com/a/5915713/735864 与System.Net.HttpRequestHeader.Host一起玩,但具有端口号它不起作用!我知道我可以解析提供的网址并执行替换操作,但是...我认为这不是实现此目的的最佳做法.
I found this post https://stackoverflow.com/a/5915713/735864 plays with System.Net.HttpRequestHeader.Host but with port number it doesn't works! I know I can parse provided url and do a Replace but... I don't think this is the best practice to achieve this.
推荐答案
System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString;
这将提供原始URI.
这篇关于在WCF REST服务中获取原始请求URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!