【问题标题】:Webapi custom JsonMediaTypeFormatterWebapi自定义JsonMediaTypeFormatter
【发布时间】:2014-12-22 09:59:03
【问题描述】:

我正在尝试创建一个自定义 JSONMediaTypeFormatter,其中将一些 json 参数发布到 webapi 调用。我需要加密从 webapi 返回的数据,因此需要编写一个自定义的 mediatypeformatter。

在我的 webapiconfig 中,我清除了所有格式化程序,只添加了自定义格式化程序。

        config.Formatters.Clear();
        config.Formatters.Add(new CipherMediaFormatter());

在我的自定义媒体类型格式化程序中,我添加了相关的标题和类型,但我仍然无法调用我的 web api。它给了我一个错误

     No MediaTypeFormatter is available to read an object of type 'Param' from content with media type        'application/json

mediatypeformatter 的代码

public class CipherMediaFormatter : JsonMediaTypeFormatter
{
    private static Type _supportedType = typeof(object);

    public CipherMediaFormatter()
    {
        this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
        this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
    }

    public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
    {
        base.SetDefaultContentHeaders(type, headers, mediaType);
        headers.ContentType = new MediaTypeHeaderValue("application/json");
    }

    public override bool CanReadType(Type type)
    {
        return true;
    }

    public override bool CanWriteType(Type type)
    {
        return true;
    }

    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
    {
        var taskSource = new TaskCompletionSource<object>();
        try
        {
            var ms = new MemoryStream();
            readStream.CopyTo(ms);
            taskSource.SetResult(ms.ToArray());
        }
        catch (Exception e)
        {
            taskSource.SetException(e);
        }
        return taskSource.Task;
    }

    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        var taskSource = new TaskCompletionSource<object>();
        try
        {
            if (value != null)
            {
                var jsonData = JsonConvert.SerializeObject(value);
                ICryptographicService cgService = new CryptographicService();
                string apiKey = string.Empty;

                string pattern = @"api\/(.*)?\/(\d+)?";
                var match = Regex.Match(HttpContext.Current.Request.RawUrl, pattern);
               ....
            }
        }
        catch (Exception e)
        {
            taskSource.SetException(e);
        }
        return taskSource.Task;
    }
}

【问题讨论】:

    标签: json asp.net-web-api


    【解决方案1】:

    问题是 ReadFromStreamAsync 导致 mediatypeformatter 出错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多