HttpContext.Current WebContentTypeMapper / GetMessageFormatForContentType中的null

我想编写一个WebContentTypeMapper并根据uri路径返回WebContentFormat依赖项。

直到现在我都使用HttpContext.Current.Request.PathInfo。 但是,如果有大于64k的正文的大型POST请求,则HttpContext.Current为空。

我发现不打算在其中使用HttpContext.Current。但是,当请求仅小一些kb时,它就可以在所有其他情况下使用。

还有其他方法可以找出GetmessageformatForContentType()中当前请求的路径吗?

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
        }
    }
}
wxp1818118 回答:HttpContext.Current WebContentTypeMapper / GetMessageFormatForContentType中的null

我现在有解决方案。由几处更改组成:

我将 componentDidMount() { axios.get('http://roocket.org/api/products') .then(response => { const { current_page,data } = response.data.data.data; this.setState({ articles: data || [] // Assign empty Array }) console.log(response) }) .catch(error => { console.log(error) }) } 更改为始终返回RawContentTypeMapper

Web.config:

  • 添加了使用WebContentFormat.Raw的绑定的副本,并删除了RawContentTypeMapper属性。因此它使用默认的映射器。

  • 通过使用RawContentTypeMapper的绑定将端点添加到每个地址(“ abc / path1” ...)。

  • 使用默认绑定将端点添加到基地址“ abc”。

IMySourceCode.cs: MySourceCode.cs:

使用contentTypeMapper创建了一个新的OperationContract。我在那里检查UriTemplate = ""中的“ abc / path1 /”,“ abc / path2 /” ...并调用原始的OperationContract方法。

为此,所有OperationContracts必须具有相同的签名(此处为MyMethod(流数据)无效)。

添加了 Global.asax 以将路径“ abc / path1”重写为“ abc / path1 /”...:

HttpContext.Current.Request.PathInfo
本文链接:https://www.f2er.com/3139308.html

大家都在问