简体   繁体   中英

PascalCase in asp.net core for only one version of the API

I'm currently developing a web API in asp.net core 1.0+. The API is using versionning, so I have a version 1 and a version 2, where the first version is to support an old android application. The problem is that I want to return the json of version 1 in PascalCase and the version 2 as camelCase, how to achieve that?

I know you can put this in your startup file, but I don't want it to be global for all API Versions.

 public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
}

Example on how I use version on my api:

[ApiVersion("1")]
[Route("api/[controller]")]
[Route("v{version:apiVersion}/[controller]")]
public class CustomerController : Controller
{
...
}

Hope someone can help

I found that you can decorate it with the following.

[JsonProperty("Name")]
public string Name { get; set; }

Then the returned value for name will be PascalCase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM