本文整理汇总了C#中System.Net.HttpListenerContext.WriteJson方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerContext.WriteJson方法的具体用法?C# HttpListenerContext.WriteJson怎么用?C# HttpListenerContext.WriteJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpListenerContext
的用法示例。
在下文中一共展示了HttpListenerContext.WriteJson方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Put
private void Put(HttpListenerContext context, string docId)
{
var json = context.ReadJson();
context.SetStatusToCreated("/docs/" + docId);
var putResult = Database.Put(docId, context.GetEtag(), json, context.Request.Headers.FilterHeaders(), GetRequestTransaction(context));
context.WriteJson(putResult);
}
示例2: Respond
public override void Respond(HttpListenerContext context)
{
switch (context.Request.HttpMethod)
{
case "GET":
context.WriteJson(Database.GetDocuments(context.GetStart(), context.GetPageSize()));
break;
case "POST":
var json = context.ReadJson();
var id = Database.Put(null, Guid.NewGuid(), json,
context.Request.Headers.FilterHeaders(),
GetRequestTransaction(context));
context.SetStatusToCreated("/docs/" + id);
context.WriteJson(id);
break;
}
}
示例3: Put
private void Put(HttpListenerContext context, string index)
{
var data = context.ReadJsonObject<IndexDefinition>();
if (data.Map == null)
{
context.SetStatusToBadRequest();
context.Write("Expected json document with 'Map' property");
return;
}
context.SetStatusToCreated("/indexes/" + index);
context.WriteJson(new { index = Database.PutIndex(index, data) });
}
示例4: OnGet
private void OnGet(HttpListenerContext context, string index)
{
var definition = context.Request.QueryString["definition"];
if ("yes".Equals(definition, StringComparison.InvariantCultureIgnoreCase))
{
context.WriteJson(new {index = Database.IndexDefinitionStorage.GetIndexDefinition(index)});
}
else
{
context.WriteJson(Database.Query(index, new IndexQuery
{
Query = context.Request.QueryString["query"],
Start = context.GetStart(),
PageSize = context.GetPageSize(),
FieldsToFetch = context.Request.QueryString.GetValues("fetch"),
SortedFields = context.Request.QueryString.GetValues("sort")
.EmptyIfNull()
.Select(x => new SortedField(x))
.ToArray()
}));
}
}
示例5: Respond
public override void Respond(HttpListenerContext context)
{
var match = urlMatcher.Match(context.Request.Url.LocalPath);
var docId = match.Groups[1].Value;
switch (context.Request.HttpMethod)
{
case "GET":
context.Response.Headers["Content-Type"] = "application/json; charset=utf-8";
var doc = Database.Get(docId,GetRequestTransaction(context));
if (doc == null)
{
context.SetStatusToNotFound();
return;
}
if (context.MatchEtag(doc.Etag))
{
context.SetStatusToNotModified();
return;
}
context.WriteData(doc.Data, doc.Metadata, doc.Etag);
break;
case "DELETE":
Database.Delete(docId, context.GetEtag(), GetRequestTransaction(context));
context.SetStatusToDeleted();
break;
case "PUT":
Put(context, docId);
break;
case "PATCH":
var patchDoc = context.ReadJsonArray();
var patchResult = Database.ApplyPatch(docId, context.GetEtag(), patchDoc, GetRequestTransaction(context));
switch (patchResult)
{
case PatchResult.DocumentDoesNotExists:
context.SetStatusToNotFound();
break;
case PatchResult.Patched:
context.WriteJson(new {patched = true});
break;
default:
throw new ArgumentOutOfRangeException("Value " + patchResult + " is not understood");
}
break;
}
}
示例6: Respond
public override void Respond(HttpListenerContext context)
{
var itemsToLoad = context.ReadJsonArray();
var results = new JArray();
Database.TransactionalStorage.Batch(actions =>
{
foreach (JToken item in itemsToLoad)
{
var documentByKey = actions.DocumentByKey(item.Value<string>(),
GetRequestTransaction(context));
if (documentByKey == null)
continue;
results.Add(documentByKey.ToJson());
}
actions.Commit();
});
context.WriteJson(results);
}
示例7: Batch
private void Batch(HttpListenerContext context)
{
var jsonCommandArray = context.ReadJsonArray();
var commands = new List<ICommandData>();
foreach (JObject jsonCommand in jsonCommandArray)
{
var key = jsonCommand["key"];
switch (jsonCommand["method"].Value<string>())
{
case "PUT":
commands.Add(new PutCommandData
{
Key = key.Value<string>(),
Etag = GetEtagFromCommand(jsonCommand),
Document = jsonCommand["document"] as JObject,
Metadata = jsonCommand["@metadata"] as JObject,
TransactionInformation = GetRequestTransaction(context)
});
break;
case "DELETE":
commands.Add(new DeleteCommandData
{
Key = key.Value<string>(),
Etag = GetEtagFromCommand(jsonCommand),
TransactionInformation = GetRequestTransaction(context)
});
continue;
default:
throw new ArgumentException("Batching only supports PUT and DELETE.");
}
}
var batchResult = Database.Batch(commands);
context.WriteJson(batchResult);
}
示例8: Respond
public override void Respond(HttpListenerContext context)
{
var txId = context.Request.QueryString["tx"];
Database.Rollback(new Guid(txId));
context.WriteJson(new { rollbacked = txId });
}
示例9: Respond
public override void Respond(HttpListenerContext context)
{
context.WriteJson(Database.GetIndexes(context.GetStart(), context.GetPageSize()));
}
示例10: Respond
public override void Respond(HttpListenerContext context)
{
context.WriteJson(Database.Statistics);
}