本文整理汇总了C#中System.Net.HttpListenerContext.JsonResponse方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerContext.JsonResponse方法的具体用法?C# HttpListenerContext.JsonResponse怎么用?C# HttpListenerContext.JsonResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpListenerContext
的用法示例。
在下文中一共展示了HttpListenerContext.JsonResponse方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TakePhoto
public bool TakePhoto(WebServer server, HttpListenerContext context)
{
try
{
if (IsRunningOnMono())
{
var process = Process.Start("raspistill",
"-o /home/pi/target/ui/" + DateTime.Now.ToString("yyyMMdd-HHmm") + ".jpg");
process.WaitForExit();
}
else
{
var filename = Path.Combine(photodir, DateTime.Now.ToString("yyyMMdd-HHmm") + ".jpg");
(new WebClient()).DownloadFile("http://unosquare.github.io/assets/embedio.png", filename);
}
var dir = new DirectoryInfo(photodir);
var files = dir.GetFiles("*.jpg").Select(x => x.Name).Take(10);
return context.JsonResponse(files);
}
catch (Exception ex)
{
return HandleError(context, ex);
}
}
示例2: HandleError
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);
}
示例3: GetHosts
public bool GetHosts(WebServer server, HttpListenerContext context)
{
try
{
var data = LocalCdn.HostEntries;
return context.JsonResponse(data);
}
catch (Exception ex)
{
return HandleError(context, ex);
}
}
示例4: Get
public bool Get(WebServer server, HttpListenerContext context)
{
try
{
var data = EntryRepository.GetAll();
var lastSegment = context.Request.Url.Segments.Last();
if (lastSegment.EndsWith("/"))
return context.JsonResponse(data.Select(x => x.Name));
if (data.Any(p => p.Name == lastSegment))
{
return context.JsonResponse(data.FirstOrDefault(p => p.Name == lastSegment));
}
throw new KeyNotFoundException("Key Not Found: " + lastSegment);
}
catch (Exception ex)
{
return HandleError(context, ex);
}
}
示例5: GetPhotos
public bool GetPhotos(WebServer server, HttpListenerContext context)
{
try
{
var dir = new DirectoryInfo(photodir);
var files = dir.GetFiles("*.jpg").Select(x => x.Name).Take(10);
return context.JsonResponse(files);
}
catch (Exception ex)
{
return HandleError(context, ex);
}
}
示例6: GetPeople
public bool GetPeople(WebServer server, HttpListenerContext context)
{
try
{
var model = context.ParseJson<GridDataRequest>();
return context.JsonResponse(model.CreateGridDataResponse(People.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);
}
}
示例7: 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(RestApiSample.People);
// otherwise, we need to parse the key and respond with the entity accordingly
int key = 0;
if (int.TryParse(lastSegment, out key) && People.Any(p => p.Key == key))
{
return context.JsonResponse(People.FirstOrDefault(p => p.Key == key));
}
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) HttpStatusCode.InternalServerError);
}
}
示例8: ButtonIo
public async Task<bool> ButtonIo(WebServer server, HttpListenerContext context)
{
var lastSegment = context.Request.Url.Segments.Last();
var bcmPinNumber = int.Parse(lastSegment);
return context.JsonResponse(new
{
count = ButtonCheck[bcmPinNumber]
});
}