我创建了一个HttpTriggered azure函数,它以大写形式返回响应。如何将其转换为驼峰案例?
return feedItems != null
? req.CreateResponse(HttpStatusCode.OK, feedItems
: req.CreateErrorResponse(HttpStatusCode.NotFound, "No news articles were found");
以上代码给出了我的资本案例。 下面的代码给我一个错误stacktrace
return feedItems != null
? req.CreateResponse(
HttpStatusCode.OK,
feedItems,
new JsonMediaTypeFormatter
{
SerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
})
: req.CreateErrorResponse(HttpStatusCode.NotFound, "No news articles were found");
堆栈跟踪
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: NewsFeedController ---> System.MissingMethodException : Method not found: 'Void System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.set_SerializerSettings(Newtonsoft.Json.JsonSerializerSettings)'.
at Juna.Zone.NewsFeed.Aggregator.NewsFeedController.Run(HttpRequestMessage req,TraceWriter log)
at lambda_method(Closure ,NewsFeedController ,Object[] )
at Microsoft.Azure.WebJobs.Host.Executors.MethodInvokerWithReturnValue`2.InvokeAsync(TReflected instance,Object[] arguments)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync[TReflected,TReturnValue](Object instance,Object[] arguments)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,ParameterHelper parameterHelper,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,ParameterHelper parameterHelper,TraceWriter traceWriter,CancellationTokenSource functionCancellationTokenSource)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)
End of inner exception
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken)
at Microsoft.Azure.WebJobs.Host.Executors.ExceptionDispatchInfoDelayedException.Throw()
at async Microsoft.Azure.WebJobs.JobHost.CallAsync(??)
at async Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(String method,Dictionary`2 arguments,CancellationToken cancellationToken)
at async Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor function,HttpRequestMessage request,CancellationToken cancellationToken)
at async Microsoft.Azure.WebJobs.Script.Host.FunctionRequestInvoker.ProcessRequestAsync(HttpRequestMessage request,CancellationToken cancellationToken,WebScriptHostManager scriptHostManager,WebHookReceiverManager webHookReceiverManager)
at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.<>c__DisplayClass3_0.<ExecuteAsync>b__0(??)
at async Microsoft.Azure.WebJobs.Extensions.Http.HttpRequestManager.ProcessRequestAsync(HttpRequestMessage request,Func`3 processRequestHandler,CancellationToken cancellationToken)
at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellationToken)
at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
at async System.Web.Http.Cors.CorsMessageHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.SystemTraceHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
at async System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
答案 0 :(得分:3)
您可以在CreateResponse函数中使用HttpConfiguration参数,如下所示
HttpConfiguration config = new HttpConfiguration();
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
var response = req.CreateResponse(HttpStatusCode.OK, data, config);
您将使用语句
添加以下内容using Newtonsoft.Json.Serialization;
using System.Web.Http;
答案 1 :(得分:1)
如果您不返回匿名类,还可以使用Newtonsoft的JsonObjectAttribute来配置Newtonsoft Json使用的命名策略。这样做的好处是可以同时序列化和反序列化。
示例类:
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class FeedItems {
public string Name { get; set; } = string.Empty;
public int Quantity { get; set; } = 0;
public string OtherProperty { get; set; } = string.Empty;
}
然后在您的HTTP触发器中,或使用Newtonsoft进行序列化的其他任何内容(即,不适用于Microsoft的DataContract序列化程序):
FeedItems feedItems = new feedItems {
Name = "Something",
Quantity = 5,
OtherProperty = "This only exists to show a property with two capitals"
};
req.CreateResponse(HttpStatusCode.OK, feedItems);
该类可以很好地用于序列化和反序列化对象,作为BrokeredMessage对象内的Azure Service Bus有效负载。与XML相比,开销更少(有最大的消息大小限制),并且在使用Service Bus Explorer来解决问题(而不是二进制文件)时易于阅读。
答案 2 :(得分:0)
将JsonPropertyAttribute添加到属性中,并通过文件顶部的#r“ Newtonsoft.Json”包含Json.NET。
#r "Newtonsoft.Json"
using Newtonsoft.Json;
并装饰属性
[JsonProperty(PropertyName = "name" )]
public string Name { get; set; }
[JsonProperty(PropertyName = "otherProp" )]
public string OtherProp { get; set; }