Пример #1
0
        public async Task Dispatch(LayimContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            var parameterValue = request.GetQuery(_parameterName);

            //如果不是Get请求,返回方法不允许
            if (!CommandMethod.Equals(request.Method, StringComparison.OrdinalIgnoreCase))
            {
                response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                await Task.FromResult(false);
            }
            //返回结果为"application/json";
            context.Response.ContentType = "application/json;charset=utf-8";
            //将参数转化为相应的类型,有 null 异常
            T value;

            if (string.IsNullOrEmpty(parameterValue))
            {
                value = default(T);
            }
            else
            {
                value = (T)Convert.ChangeType(parameterValue, typeof(T));
            }
            //执行具体处理函数
            var result = _command(context, value);
            //序列化结果
            var json = context.Options.Serializer.SerializeObject(result);
            //返回
            await context.Response.WriteAsync(json);
        }
        public static IDictionary <string, object> GetOwinEnvironment(this LayimContext context)
        {
            Error.ThrowIfNull(context, nameof(context));

            var owinContext = context as OwinLayimContext;

            return(owinContext.Environment);
        }
Пример #3
0
        public Task Dispatch(LayimContext context)
        {
            context.Response.ContentType = _contentType;
            context.Response.SetExpire(DateTimeOffset.Now.AddYears(1));

            WriteResponse(context.Response);

            return(Task.FromResult(true));
        }
Пример #4
0
        internal void Assign(LayimContext context)
        {
            Request  = context.Request;
            Response = context.Response;
            Storage  = context.Storage;
            AppPath  = context.Options.AppPath;

            Url = new UrlHelper(context);
        }
Пример #5
0
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Dispatch(LayimContext context)
        {
            context.Response.ContentType = "text/html";
            //根据路径匹配到相应的页面
            var page = _pageFunc(context.UriMatch);

            page.Assign(context);
            //输出该页面的值
            return(context.Response.WriteAsync(page.ToString()));
        }
Пример #6
0
        public async Task Dispatch(LayimContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            if (!CommandMethod.Equals(request.Method, StringComparison.OrdinalIgnoreCase))
            {
                response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                await Task.FromResult(false);
            }

            context.Response.ContentType = "application/json";

            var result = _command(context);

            var json = context.Options.Serializer.SerializeObject(result);

            await context.Response.WriteAsync(json);
        }
Пример #7
0
        /// <summary>
        /// 实现方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Dispatch(LayimContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            if (!CommandMethod.Equals(request.Method, StringComparison.OrdinalIgnoreCase))
            {
                response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                return(Task.FromResult(false));
            }

            //执行命令
            if (_command(context))
            {
                response.StatusCode = (int)HttpStatusCode.OK;
            }
            else
            {
                response.StatusCode = 422;
            }
            return(Task.FromResult(true));
        }
Пример #8
0
        public UrlHelper(LayimContext context)
        {
            Error.ThrowIfNull(context, nameof(context));

            _context = context;
        }