gpt4 book ai didi

azure - 用于应用程序功能的基于文件的工件

转载 作者:行者123 更新时间:2023-12-03 01:06:59 26 4
gpt4 key购买 nike

我是应用程序功能的新手。但我已经成功创建了一个自定义应用程序函数,它将接受一些 JSON。

当我的 JSON 上传时,我的应用程序功能将需要根据架构对其进行验证。 JSON 将位于 POST 请求的正文中,以更准确地描述正在发生的情况。

http://www.newtonsoft.com/json/help/html/JsonSchema.htm

这是验证的基本代码:

string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";

JsonSchema schema = JsonSchema.Parse(schemaJson);

JObject person = JObject.Parse(@"{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}");


IList<string> messages;
bool valid = person.IsValid(schema, out messages);

上面的代码有

string schemaJson

我想保存一个本地文件以供我加载和验证。

实际上,我将有多个文件,http-request 中的参数之一将触发我使用哪个文件进行 json-schema-validate。

假设“mycustomerid”在 http 请求的 header (或查询字符串或其他内容)中传递,并且 mycustomerid 的值将驱动我想要验证输入 json 的模式。

所以我会有几个文件。

customer_1.jsonschema 
customer_2.jsonschema
customer_3.jsonschema

存储这些对于我相当简单的应用程序功能来说是必需的文件的最佳实践是什么?

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{

附加:

这是我使用此处接受的答案以及我在这里找到的内容的最终答案:How to get local file system path in azure websites

            string rootDirectory = string.Empty;
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME")))
{
/* running in azure */
rootDirectory = Environment.GetEnvironmentVariable("HOME") + "\\site\\wwwroot";
}
else
{
/* in visual studio, local debugging */
rootDirectory = ".";
}
string path = rootDirectory + "\\MySubFolder\\MyFile.jsonschema";

最佳答案

步骤 1. 使用 Kudu 控制台在“D:\home\site\wwwroot\HttpTriggerCSharp1”中创建文件“schema.json” https://github.com/projectkudu/kudu/wiki/Kudu-console

第 2 步:将文件内容读取为字符串的 HttpTrigger 代码:

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
var path = Environment.GetEnvironmentVariable("HOME").ToString() + "\\site\\wwwroot\\HttpTriggerCSharp1\\schema.json";

string readText = File.ReadAllText(path);

log.Info(readText);
return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}

关于azure - 用于应用程序功能的基于文件的工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com