ASP.Net非核心Web API - 将 Camel case 格式的 JSON 答案更改为 Pascal case 引导程序JsonSerializer

问题描述

一周前我被介绍到一个已经存在的 C# ASP.Net Web API 项目。它以 JSON 和 Pascal Case 形式返回所有数据,这些数据由使用 React.Js 的网站使用,这些数据区分大小写。

上周,在 API 项目上的提交几乎没有任何变化(它只为解决方案中的 Web 客户端项目添加了翻译)之后,突然该项目开始在 Camel 案例中返回 JSON而不是在 Pascal Case 中。

问题不像 this post,我不发送 API 骆驼而不是 pascal,API 发送骆驼而不是 pascal。

我一直在寻找如何修复它,但该项目有两个特殊性使得很难找到答案:

  • 该项目使用 ASP.Net 而不是 ASP.Net Core,使得 thisthis 之类的帖子没有帮助
  • 该项目使用 NancyFx 2.0.0,因此它没有任何启动文件(如核心项目中的启动类或 .asax 文件),但使用自定义引导程序(请参阅下面的代码

引导程序

public class AppBootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container,IPipelines pipelines)
    {
        log4net.Config.XmlConfigurator.Configure();

        base.ApplicationStartup(container,pipelines);

        pipelines.BeforeRequest.AddItemToStartOfPipeline(ctx =>
        {
            if (ctx != null)
            {
                Log.Request(ctx.Request.GetHashCode(),ctx.Request.Method,ctx.Request.Path,ctx.Request.UserHostAddress,ctx.Request.Headers.UserAgent);
            }

            return null;
        });

        pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
        {
            if (ctx != null)
            {
                Log.Response(ctx.Request.GetHashCode(),ctx.Response.StatusCode);
            }
        });
    }

    protected override void RequestStartup(TinyIoCContainer container,IPipelines pipelines,NancyContext context)
    {
        pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
        {
            ctx.Response.WithHeader("Access-Control-Allow-Origin","*")
                .WithHeader("Access-Control-Allow-Methods","POST,GET,PUT,DELETE,HEAD,OPTIONS")
                .WithHeader("Access-Control-Allow-Headers","Accept,Origin,Content-type,Authorization");
        });

        // Gzip management,useless for this post
}

JsonSerializer

internal class JsonNetSerializer : ISerializer
{
    private readonly JsonSerializer _serializer;

    /// <summary>
    /// Initializes a new instance of the <see cref="JsonNetSerializer"/> class.
    /// </summary>
    public JsonNetSerializer()
    {
        _serializer = JsonSerializer.CreateDefault();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="JsonNetSerializer"/> class,/// with the provided <paramref name="serializer"/>.
    /// </summary>
    /// <param name="serializer">Json converters used when serializing.</param>
    public JsonNetSerializer(JsonSerializer serializer)
    {
        _serializer = serializer;
    }

    /// <summary>
    /// Whether the serializer can serialize the content type
    /// </summary>
    /// <param name="mediarange">Content type to serialise</param>
    /// <returns>True if supported,false otherwise</returns>
    public bool CanSerialize(Mediarange mediarange)
    {
        return JsonHelpers.IsJsonType(mediarange);
    }

    /// <summary>
    /// Gets the list of extensions that the serializer can handle.
    /// </summary>
    /// <value>An <see cref="IEnumerable{T}"/> of extensions if any are available,otherwise an empty enumerable.</value>
    public IEnumerable<string> Extensions
    {
        get { yield return "json"; }
    }

    /// <summary>
    /// Serialize the given model with the given contentType
    /// </summary>
    /// <param name="mediarange">Content type to serialize into</param>
    /// <param name="model">Model to serialize</param>
    /// <param name="outputStream">Output stream to serialize to</param>
    /// <returns>Serialised object</returns>
    public void Serialize<TModel>(Mediarange mediarange,TModel model,Stream outputStream)
    {
        using (var writer = new JsonTextWriter(new StreamWriter(new UnclosableStreamWrapper(outputStream))))
        {
            _serializer.Serialize(writer,model);
        }
    }
}

我一直在寻找解决方案,很久以来我发现一些网站在谈论 Owin,但似乎我必须使用基于 owin 的服务器(如 Nowin)来托管 ASP.Net 应用程序。问题是我实际上从未使用过/听说过它,而且我没有时间学习如何使用它。最重要的是,我完全不确定 Nowin 的 Start<TContext>() 函数对于修改 API 的返回格式是否有用...

我发现的所有其他解决方案都适用于 ASP.Net Core 技术,但没有适用于 ASP.Net

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...