C#Firebase Restful Post生成唯一密钥 [英] C# Firebase Restful Post generate unique key
问题描述
下面是我用来对Firebase数据库执行POST请求的代码. 我该怎么做,以允许子序列POST请求按升序排列?我不希望Firebase自动生成唯一ID.
Below is my code that i used to do a POST request to firebase database. What can i do, to allow the subsequence POST request to be in ascending order? I do not want the firebase auto generated unique ID.
可以在此处查看示例. 在此处输入图片描述
Example can be viewed here. enter image description here
public void postEachFirebaseMessage(string dbcolName)
{
var json = Newtonsoft.Json.JsonConvert.SerializeObject(new
{
user = "UserNameValue",
message = "MessageValue",
});
var request = WebRequest.CreateHttp(@"" + config.BasePath + dbcolName + ".json?auth=" + config.AuthSecret);
request.Method = "POST";
request.ContentType = "application/json";
var buffer = Encoding.UTF8.GetBytes(json);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = request.GetResponse();
json = (new StreamReader(response.GetResponseStream())).ReadToEnd();
}
推荐答案
我不希望Firebase自动生成唯一的ID.
I do not want the firebase auto generated unique ID.
如果您不希望Firebase为您自动生成唯一密钥,则可以生成自己的唯一密钥,然后生成PUT
数据到该完整URL.像这样:
If you don't want Firebase to auto-generate a unique key for you, you can generate your own unique key and then PUT
data to that full URL. So something like this:
var request = WebRequest.CreateHttp(@"" + config.BasePath + dbcolName + "/yourOwnUniqueKey" + ".json?auth=" + config.AuthSecret);
request.Method = "PUT";
但是如前所述:您将需要确保自己的密钥是唯一的.这是一个完全有效的方案,但是您必须自己编写代码以生成唯一密钥.
But as said: you will need to ensure yourself that the key is unique. That is a completely valid scenario, but you will have to write the code for generating the unique keys yourself.
如果您想知道Firebase为什么不简单使用0
,1
,2
之类的键,建议阅读此博客文章:
If you wonder why Firebase doesn't simply use keys like 0
, 1
, 2
, I recommend reading this blog post: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
这篇关于C#Firebase Restful Post生成唯一密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!