本文整理汇总了C#中HttpListenerContext.JsonResponse方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerContext.JsonResponse方法的具体用法?C# HttpListenerContext.JsonResponse怎么用?C# HttpListenerContext.JsonResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpListenerContext
的用法示例。
在下文中一共展示了HttpListenerContext.JsonResponse方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPeople
public bool GetPeople(WebServer server, HttpListenerContext context)
{
try
{
// read the last segment
var lastSegment = context.Request.Url.Segments.Last();
// if it ends with a / means we need to list people
if (lastSegment.EndsWith("/"))
return context.JsonResponse(PeopleRepository.Database);
// otherwise, we need to parse the key and respond with the entity accordingly
int key;
if (int.TryParse(lastSegment, out key) && PeopleRepository.Database.Any(p => p.Key == key))
{
return context.JsonResponse(PeopleRepository.Database.FirstOrDefault(p => p.Key == key));
}
throw new KeyNotFoundException("Key Not Found: " + lastSegment);
}
catch (Exception ex)
{
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
return context.JsonResponse(ex);
}
}
示例2: GetPeople
public bool GetPeople(WebServer server, HttpListenerContext context)
{
try
{
// read the last segment
var lastSegment = context.Request.Url.Segments.Last();
// if it ends with a / means we need to list people
if (lastSegment.EndsWith("/"))
return context.JsonResponse(_dbContext.People.SelectAll());
// if it ends with "first" means we need to show first record of people
if (lastSegment.EndsWith("first"))
return context.JsonResponse(_dbContext.People.SelectAll().First());
// otherwise, we need to parse the key and respond with the entity accordingly
int key = 0;
if (int.TryParse(lastSegment, out key))
{
var single = _dbContext.People.Single(key);
if (single != null)
return context.JsonResponse(single);
}
throw new KeyNotFoundException("Key Not Found: " + lastSegment);
}
catch (Exception ex)
{
// here the error handler will respond with a generic 500 HTTP code a JSON-encoded object
// with error info. You will need to handle HTTP status codes correctly depending on the situation.
// For example, for keys that are not found, ou will need to respond with a 404 status code.
return HandleError(context, ex, (int)System.Net.HttpStatusCode.InternalServerError);
}
}
示例3: GetPerson
public bool GetPerson(WebServer server, HttpListenerContext context, int id)
{
try
{
if (PeopleRepository.Database.Any(p => p.Key == id))
{
return context.JsonResponse(PeopleRepository.Database.FirstOrDefault(p => p.Key == id));
}
throw new KeyNotFoundException("Key Not Found: " + id);
}
catch (Exception ex)
{
context.Response.StatusCode = errorCode;
return context.JsonResponse(ex);
}
}
示例4: GetPersonAsync
public async Task<bool> GetPersonAsync(WebServer server, HttpListenerContext context, int id)
{
try
{
await Task.Delay(TimeSpan.FromSeconds(1));
if (PeopleRepository.Database.Any(p => p.Key == id))
{
return context.JsonResponse(PeopleRepository.Database.FirstOrDefault(p => p.Key == id));
}
throw new KeyNotFoundException("Key Not Found: " + id);
}
catch (Exception ex)
{
context.Response.StatusCode = errorCode;
return context.JsonResponse(ex);
}
}
示例5: PostPeople
public bool PostPeople(WebServer server, HttpListenerContext context)
{
try
{
var content = context.ParseJson<Person>();
return context.JsonResponse(content);
}
catch (Exception ex)
{
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
return context.JsonResponse(ex);
}
}
示例6: GetPerson
public bool GetPerson(WebServer server, HttpListenerContext context)
{
try
{
// read the middle segment
var segment = context.Request.Url.Segments.Reverse().Skip(1).First().Replace("/", "");
// otherwise, we need to parse the key and respond with the entity accordingly
int key;
if (int.TryParse(segment, out key) && PeopleRepository.Database.Any(p => p.Key == key))
{
return context.JsonResponse(PeopleRepository.Database.FirstOrDefault(p => p.Key == key));
}
throw new KeyNotFoundException("Key Not Found: " + segment);
}
catch (Exception ex)
{
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
return context.JsonResponse(ex);
}
}
示例7: PostEcho
public bool PostEcho(WebServer server, HttpListenerContext context)
{
try
{
var content = context.RequestFormDataDictionary();
return context.JsonResponse(content);
}
catch (Exception ex)
{
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
return context.JsonResponse(ex);
}
}
示例8: PostPeople
public async Task<bool> PostPeople(WebServer server, HttpListenerContext context)
{
try
{
var model = context.ParseJson<GridDataRequest>();
var data = await _dbContext.People.SelectAllAsync();
return context.JsonResponse(model.CreateGridDataResponse(data.AsQueryable()));
}
catch (Exception ex)
{
// here the error handler will respond with a generic 500 HTTP code a JSON-encoded object
// with error info. You will need to handle HTTP status codes correctly depending on the situation.
// For example, for keys that are not found, ou will need to respond with a 404 status code.
return HandleError(context, ex);
}
}
示例9: HandleError
/// <summary>
/// Handles the error returning an error status code and json-encoded body.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="ex">The ex.</param>
/// <param name="statusCode">The HTTP status code.</param>
/// <returns></returns>
protected bool HandleError(HttpListenerContext context, Exception ex, int statusCode = 500)
{
var errorResponse = new
{
Title = "Unexpected Error",
ErrorCode = ex.GetType().Name,
Description = ex.ExceptionMessage(),
};
context.Response.StatusCode = statusCode;
return context.JsonResponse(errorResponse);
}
示例10: Echo
public bool Echo(WebServer server, HttpListenerContext context)
{
try
{
var content = context.RequestFormDataDictionary();
return context.JsonResponse(content);
}
catch (Exception ex)
{
return HandleError(context, ex);
}
}