C# HttpContext.WebContentTypeMapper/GetMessageFormatForContentType中当前为null
我想编写一个C# HttpContext.WebContentTypeMapper/GetMessageFormatForContentType中当前为null,c#,web-services,iis,C#,Web Services,Iis,我想编写一个WebContentTypeMapper,它返回依赖于uri路径的WebContentFormat 到目前为止,我使用的是HttpContext.Current.Request.PathInfo。 但是,如果存在正文超过64k的大型POST请求,则HttpContext.Current为空 我发现HttpContext.Current不打算在那里使用。但当请求只有几kb时,它在所有其他情况下都有效 在GetMessageFormatForContentType()中是否有其他方法可以
WebContentTypeMapper
,它返回依赖于uri路径的WebContentFormat
到目前为止,我使用的是HttpContext.Current.Request.PathInfo
。
但是,如果存在正文超过64k的大型POST请求,则HttpContext.Current
为空
我发现HttpContext.Current不打算在那里使用。但当请求只有几kb时,它在所有其他情况下都有效
在GetMessageFormatForContentType()
中是否有其他方法可以找到当前请求的路径
我现在有了解决办法。由几个变化组成: 我将
RawContentTypeMapper
更改为始终返回WebContentFormat.Raw
Web.config:
- 添加了使用
的绑定副本,并删除了RawContentTypeMapper
属性。因此它使用默认映射器contentTypeMapper
- 使用使用RawContentTypeMapper的绑定向每个地址(“abc/path1”…)添加了一个端点
- 使用默认绑定将端点添加到基址“abc”
UriTemplate=”“
创建了一个新的OperationContract
。在那里,我检查HttpContext.Current.Request.PathInfo
中的“abc/path1/”、“abc/path2/”。。。并调用原始OperationContract方法
因此,所有OperationContract必须具有相同的签名(此处为void MyMethod(流数据))
添加了Global.asax以将路径“abc/path1”重写为“abc/path1/”…:
您是否尝试过扩展IIS管理器->配置编辑器->system.webServer/serverRuntime/uploadReadAheadSize的值?因为这个值的极限是46kI,我不知道。它的默认值为49152。
public class RawContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
string pathInfo;
try
{
pathInfo = HttpContext.Current.Request.PathInfo;
}
catch (NullReferenceException e)
{
// This happens for large POST requests
pathInfo = ...please help!
}
switch (pathInfo)
{
case "/abc/path1":
case "/abc/path2":
return WebContentFormat.Raw;
default:
return WebContentFormat.Default; // No change to default behavior
}
}
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
var pathInfo = HttpContext.Current.Request.PathInfo.ToLowerInvariant();
if (pathInfo.StartsWith("/abc/"))
{
foreach (var s in new[] { "/abc/path1", "/abc/path2" })
{
if (pathInfo == s)
{
var rawUrl = HttpContext.Current.Request.RawUrl.ToLower(CultureInfo.InvariantCulture);
HttpContext.Current.RewritePath($"{rawUrl}/");
break;
}
}
}
}