“application/json” constant in .NET framework

限于喜欢 提交于 2019-12-04 08:46:48

问题


I've just declared a constant for the "application/json" content type in one of my classes.

public const string JsonContentType = "application/json";

I'm not sure it is a good practice.

Does .NET framework have a predefined const for "application/json"?


回答1:


While there are some MIME constants defined in MediaTypeNames (see here), there no constant for "application/json".

Putting additional content types in a shared const is probably best practice, better than defining them in string literals a million times throughout your code at least.

Plus it gives you the flexibility of using new/custom MIME types, which a specific .NET version might not have.




回答2:


In order to add an up-to-date answer: since dotnet core 2.1 MediaTypeNames.Application.Json has been defined.

See https://github.com/dotnet/corefx/pull/26701 for the changeset.



来源:https://stackoverflow.com/questions/20655938/application-json-constant-in-net-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!