1
我不能用靜態JsonConvert
設置,並在我的過濾器,我需要根據當前MvcJsonOptions
格式化字符串:如何在AspNet Core 1.1中注入MvcJsonOptions?
services.AddMvc().AddJsonOptions(x =>
{
x.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
class ReturnBadRequestForInvalidModelFilter : IAsyncActionFilter
{
private readonly MvcJsonOptions _options;
public ReturnBadRequestForInvalidModelFilter(MvcJsonOptions options)
{
_options = options;
}
...
private string FormatPropertyName(string key)
{
if (string.IsNullOrEmpty(key))
return key;
return _options.SerializerSettings.ContractResolver is CamelCasePropertyNamesContractResolver
? char.ToLowerInvariant(key[0]) + key.Substring(1)
: key;
}
這是否不行?它錯誤嗎?不清楚你的問題是什麼,對不起。 –
它失敗了,'MvcJsonOptions'不是服務。 – smg