"application/json" .NET框架中的常量 [英] "application/json" constant in .NET framework

查看:198
本文介绍了"application/json" .NET框架中的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在我的一个类中为"application/json"内容类型声明了一个常量.

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.

.NET框架是否为"application/json"提供了预定义的常量?

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

推荐答案

MediaTypeNames中定义了一些 MIME常量(请参见

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

将其他内容类型放入共享const中可能是最佳实践,这比在整个代码中至少一百万次在字符串文字中定义它们更好.

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.

此外,它还为您提供了使用新的/自定义MIME类型的灵活性,而特定的.NET版本可能没有这种类型.

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

这篇关于"application/json" .NET框架中的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆