当前位置: 首页>>代码示例>>C#>>正文


C# HttpListenerContext.JsonResponse方法代码示例

本文整理汇总了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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:27,代码来源:TestController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:35,代码来源:PeopleController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:17,代码来源:TestRegexController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:19,代码来源:TestRegexController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:14,代码来源:TestController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:23,代码来源:TestController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:14,代码来源:TestController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:17,代码来源:PeopleController.cs

示例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);
        }
开发者ID:unosquare,项目名称:embedio,代码行数:19,代码来源:PeopleController.cs

示例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);
            }
        }
开发者ID:unosquare,项目名称:embedio,代码行数:13,代码来源:PeopleController.cs


注:本文中的HttpListenerContext.JsonResponse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。