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


C# IRequest.GetItem方法代码示例

本文整理汇总了C#中IRequest.GetItem方法的典型用法代码示例。如果您正苦于以下问题:C# IRequest.GetItem方法的具体用法?C# IRequest.GetItem怎么用?C# IRequest.GetItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IRequest的用法示例。


在下文中一共展示了IRequest.GetItem方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SerializeToStream

        public void SerializeToStream(IRequest request, object response, IResponse httpRes)
        {
            var httpResult = request.GetItem("HttpResult") as IHttpResult;
            if (httpResult != null && httpResult.Headers.ContainsKey(HttpHeaders.Location) 
                && httpResult.StatusCode != System.Net.HttpStatusCode.Created)  
                return;

            try
            {
                if (httpRes.StatusCode >= 400)
                {
                    var responseStatus = response.GetResponseStatus();
                    request.Items[ErrorStatusKey] = responseStatus;
                }

                if (AppHost.ViewEngines.Any(x => x.ProcessRequest(request, httpRes, response))) return;
            }
            catch (Exception ex)
            {
                if (httpRes.StatusCode < 400)
                    throw;

                //If there was an exception trying to render a Error with a View, 
                //It can't handle errors so just write it out here.
                response = DtoUtils.CreateErrorResponse(request.Dto, ex);
            }

            if (request.ResponseContentType != MimeTypes.Html
                && request.ResponseContentType != MimeTypes.JsonReport) return;

            var dto = response.GetDto();
            var html = dto as string;
            if (html == null)
            {
                // Serialize then escape any potential script tags to avoid XSS when displaying as HTML
                var json = JsonDataContractSerializer.Instance.SerializeToString(dto) ?? "null";
                json = json.Replace("<", "&lt;").Replace(">", "&gt;");

                var url = request.AbsoluteUri
                    .Replace("format=html", "")
                    .Replace("format=shtm", "")
                    .TrimEnd('?', '&');

                url += url.Contains("?") ? "&" : "?";

                var now = DateTime.UtcNow;
                var requestName = request.OperationName ?? dto.GetType().GetOperationName();

                html = HtmlTemplates.GetHtmlFormatTemplate()
                    .Replace("${Dto}", json)
                    .Replace("${Title}", string.Format(TitleFormat, requestName, now))
                    .Replace("${MvcIncludes}", MiniProfiler.Profiler.RenderIncludes().ToString())
                    .Replace("${Header}", string.Format(HtmlTitleFormat, requestName, now))
                    .Replace("${ServiceUrl}", url)
                    .Replace("${Humanize}", Humanize.ToString().ToLower());
            }

            var utf8Bytes = html.ToUtf8Bytes();
            httpRes.OutputStream.Write(utf8Bytes, 0, utf8Bytes.Length);
        }
开发者ID:HarmenGrosseDeters,项目名称:ServiceStack,代码行数:60,代码来源:HtmlFormat.cs

示例2: HandleCacheResponses

        public void HandleCacheResponses(IRequest req, IResponse res, object response)
        {
            if (req.IsInProcessRequest())
                return;

            if (response is Exception || res.StatusCode >= 300)
                return;

            var cacheInfo = req.GetItem(Keywords.CacheInfo) as CacheInfo;
            if (cacheInfo != null && cacheInfo.CacheKey != null)
            {
                if (CacheAndWriteResponse(cacheInfo, req, res, response))
                    return;
            }

            var httpResult = response as HttpResult;
            if (httpResult == null)
                return;

            cacheInfo = httpResult.ToCacheInfo();

            if ((req.Verb != HttpMethods.Get && req.Verb != HttpMethods.Head) ||
                (httpResult.StatusCode != HttpStatusCode.OK && httpResult.StatusCode != HttpStatusCode.NotModified))
                return;

            if (httpResult.LastModified != null)
                httpResult.Headers[HttpHeaders.LastModified] = httpResult.LastModified.Value.ToUniversalTime().ToString("r");

            if (httpResult.ETag != null)
                httpResult.Headers[HttpHeaders.ETag] = httpResult.ETag.Quoted();

            if (httpResult.Expires != null)
                httpResult.Headers[HttpHeaders.Expires] = httpResult.Expires.Value.ToUniversalTime().ToString("r");

            if (httpResult.Age != null)
                httpResult.Headers[HttpHeaders.Age] = httpResult.Age.Value.TotalSeconds.ToString(CultureInfo.InvariantCulture);

            var alreadySpecifiedCacheControl = httpResult.Headers.ContainsKey(HttpHeaders.CacheControl);
            if (!alreadySpecifiedCacheControl)
            {
                var cacheControl = BuildCacheControlHeader(cacheInfo);
                if (cacheControl != null)
                    httpResult.Headers[HttpHeaders.CacheControl] = cacheControl;
            }

            if (req.ETagMatch(httpResult.ETag) || req.NotModifiedSince(httpResult.LastModified))
            {
                res.EndNotModified();
            }
        }
开发者ID:ServiceStack,项目名称:ServiceStack,代码行数:50,代码来源:HttpCacheFeature.cs

示例3: SerializeToStream

		public void SerializeToStream(IRequest request, object response, IResponse httpRes)
		{
            var httpResult = request.GetItem("HttpResult") as IHttpResult;
            if (httpResult != null && httpResult.Headers.ContainsKey(HttpHeaders.Location))
                return;

            if (AppHost.ViewEngines.Any(x => x.ProcessRequest(request, httpRes, response))) return;

            if (request.ResponseContentType != MimeTypes.Html
                && request.ResponseContentType != MimeTypes.JsonReport) return;

		    var dto = response.GetDto();
		    var html = dto as string;
            if (html == null)
            {
                // Serialize then escape any potential script tags to avoid XSS when displaying as HTML
                var json = JsonDataContractSerializer.Instance.SerializeToString(dto) ?? "null";
                json = json.Replace("<", "&lt;").Replace(">", "&gt;");

                var url = request.AbsoluteUri
                    .Replace("format=html", "")
                    .Replace("format=shtm", "")
                    .TrimEnd('?', '&');

                url += url.Contains("?") ? "&" : "?";

                var now = DateTime.UtcNow;
                var requestName = request.OperationName ?? dto.GetType().Name;

                html = HtmlTemplates.GetHtmlFormatTemplate()
                    .Replace("${Dto}", json)
                    .Replace("${Title}", string.Format(TitleFormat, requestName, now))
                    .Replace("${MvcIncludes}", MiniProfiler.Profiler.RenderIncludes().ToString())
                    .Replace("${Header}", string.Format(HtmlTitleFormat, requestName, now))
                    .Replace("${ServiceUrl}", url);
            }

			var utf8Bytes = html.ToUtf8Bytes();
			httpRes.OutputStream.Write(utf8Bytes, 0, utf8Bytes.Length);
		}
开发者ID:0815sugo,项目名称:ServiceStack,代码行数:40,代码来源:HtmlFormat.cs

示例4: ExecuteRazorPage

        public IRazorView ExecuteRazorPage(IRequest httpReq, IResponse httpRes, object model, RazorPage razorPage)
        {
            if (razorPage == null)
            {
                httpRes.StatusCode = (int)HttpStatusCode.NotFound;
                return null;
            }

            var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage);

            var includeLayout = !(httpReq.GetParam(QueryStringFormatKey) ?? "").Contains(NoTemplateFormatValue);
            if (includeLayout)
            {
                var result = ExecuteRazorPageWithLayout(razorPage, httpReq, httpRes, model, page, () =>
                    httpReq.GetItem(LayoutKey) as string
                    ?? page.Layout
                    ?? DefaultLayoutName);

                if (httpRes.IsClosed)
                    return result != null ? result.Item1 : null;

                using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom))
                {
                    writer.Write(result.Item2);
                }
                return result.Item1;
            }

            using (var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom))
            {
                page.WriteTo(writer);
            }
            return page;
        }
开发者ID:BilliamBrown,项目名称:ServiceStack,代码行数:34,代码来源:RazorPageResolver.cs

示例5: ResolveViewPage

        public RazorPage ResolveViewPage(IRequest httpReq, object model)
        {
            var viewName = httpReq.GetItem(ViewKey) as string;
            if (viewName != null)
                return viewManager.GetViewPage(viewName);

            return viewManager.GetViewPage(httpReq.OperationName) // Request DTO
                   ?? viewManager.GetViewPage(model.GetType().Name); // Response DTO
        }
开发者ID:BilliamBrown,项目名称:ServiceStack,代码行数:9,代码来源:RazorPageResolver.cs

示例6: SerializeSoapToStream

        private static void SerializeSoapToStream(IRequest req, object response, MessageVersion defaultMsgVersion, Stream stream)
        {
            var requestMsg = req.GetItem("SoapMessage") as Message;
            var msgVersion = requestMsg != null
                ? requestMsg.Version
                : defaultMsgVersion;

            var noMsgVersion = requestMsg == null || requestMsg.Headers.Action == null;

            var responseMsg = CreateResponseMessage(response, msgVersion, req.Dto.GetType(), noMsgVersion);
            SetErrorStatusIfAny(req.Response, responseMsg, req.Response.StatusCode);

            using (var writer = CreateXmlWriter(stream))
            {
                responseMsg.WriteMessage(writer);
            }
        }
开发者ID:softwx,项目名称:ServiceStack,代码行数:17,代码来源:SoapHandler.cs

示例7: GetDbConnection

        public virtual IDbConnection GetDbConnection(IRequest req = null)
        {
            var dbFactory = Container.TryResolve<IDbConnectionFactory>();

            ConnectionInfo connInfo;
            if (req != null && (connInfo = req.GetItem(Keywords.DbInfo) as ConnectionInfo) != null)
            {
                var dbFactoryExtended = dbFactory as IDbConnectionFactoryExtended;
                if (dbFactoryExtended == null)
                    throw new NotSupportedException("ConnectionInfo can only be used with IDbConnectionFactoryExtended");

                if (connInfo.ConnectionString != null && connInfo.ProviderName != null)
                    return dbFactoryExtended.OpenDbConnectionString(connInfo.ConnectionString, connInfo.ProviderName);

                if (connInfo.ConnectionString != null)
                    return dbFactoryExtended.OpenDbConnectionString(connInfo.ConnectionString);

                if (connInfo.NamedConnection != null)
                    return dbFactoryExtended.OpenDbConnection(connInfo.NamedConnection);
            }

            return dbFactory.OpenDbConnection();
        }
开发者ID:yuinlin,项目名称:ServiceStack,代码行数:23,代码来源:ServiceStackHost.Runtime.cs

示例8: GetHeader

        /*
         *  If the user is providing the date via the custom header then the server
         *  will use that for the hash. Otherwise we check for the default "Date" header.
         *  This is nessary since some consumers can't control the date header in their web requests
         */
        /*  Implementation to get SOAP OR REST headers */
        private static string GetHeader(IRequest request, string header)
        {
            string headerValue = null;
            if (request.GetItem("SoapMessage") != null)
            {
                var soapHeader = request.GetSoapMessage().Headers.SingleOrDefault(x => x.Name.EqualsIgnoreCase(header));
                if (soapHeader != null && !string.IsNullOrEmpty(soapHeader.ToString()))
                {
                    var doc = new XmlDocument();
                    doc.LoadXml(soapHeader.ToString());
                    headerValue = doc.FirstChild.InnerText;
                }
            }

            var restHeader = request.Headers[header];
            if (!string.IsNullOrEmpty(restHeader))
            {
                headerValue = restHeader;
            }
            return headerValue;
        }
开发者ID:CodeRevver,项目名称:notekeeper-api,代码行数:27,代码来源:ApiSignature.cs

示例9: ResolveContentPage

        public RazorPage ResolveContentPage(IRequest httpReq)
        {
            var viewName = httpReq.GetItem(ViewKey) as string;
            if (viewName != null)
                return viewManager.GetContentPage(viewName);

            return viewManager.GetContentPage(httpReq.PathInfo);
        }
开发者ID:softwx,项目名称:ServiceStack,代码行数:8,代码来源:RazorPageResolver.cs

示例10: FindRazorPage

 private RazorPage FindRazorPage(IRequest httpReq, object model)
 {
     var viewName = httpReq.GetItem(ViewKey) as string;
     if (viewName != null)
     {
         return this.viewManager.GetPageByName(viewName, httpReq, model);
     }
     var razorPage = this.viewManager.GetPageByName(httpReq.OperationName) //Request DTO
                  ?? this.viewManager.GetPage(httpReq, model); // Response DTO
     return razorPage;
 }
开发者ID:0815sugo,项目名称:ServiceStack,代码行数:11,代码来源:RazorPageResolver.cs

示例11: SerializeSoapToStream

        private static void SerializeSoapToStream(IRequest req, object response, MessageVersion defaultMsgVersion, Stream stream)
        {
            var requestMsg = req.GetItem(Keywords.SoapMessage) as Message;
            var msgVersion = requestMsg != null
                ? requestMsg.Version
                : defaultMsgVersion;

            var noMsgVersion = requestMsg == null || requestMsg.Headers.Action == null;

            var responseMsg = CreateResponseMessage(response, msgVersion, req.Dto.GetType(), noMsgVersion);
            SetErrorStatusIfAny(req.Response, responseMsg, req.Response.StatusCode);

            HostContext.AppHost.WriteSoapMessage(req, responseMsg, stream);
        }
开发者ID:HAlakeshwar,项目名称:ServiceStack,代码行数:14,代码来源:SoapHandler.cs

示例12: ExecuteRazorPage

        public IRazorView ExecuteRazorPage(IRequest httpReq, IResponse httpRes, object model, RazorPage razorPage)
        {
            if (razorPage == null)
            {
                httpRes.StatusCode = (int)HttpStatusCode.NotFound;
                return null;
            }

            try
            {
                var page = CreateRazorPageInstance(httpReq, httpRes, model, razorPage);

                var includeLayout = !(httpReq.GetParam(Keywords.Format) ?? "").Contains(Keywords.Bare);
                if (includeLayout)
                {
                    var result = ExecuteRazorPageWithLayout(razorPage, httpReq, httpRes, model, page, () =>
                        httpReq.GetItem(LayoutKey) as string
                        ?? page.Layout
                        ?? DefaultLayoutName);

                    if (httpRes.IsClosed)
                        return result != null ? result.Item1 : null;

                    var layoutWriter = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom);
                    var html = HtmlFilter(result.Item2);

                    layoutWriter.Write(html);
                    layoutWriter.Flush();
                    return result.Item1;
                }

                var writer = new StreamWriter(httpRes.OutputStream, UTF8EncodingWithoutBom);
                page.WriteTo(writer);
                writer.Flush();

                return page;
            }
            finally
            {
                RequestContext.Instance.ReleaseDisposables();
            }
        }
开发者ID:jrmitch120,项目名称:ServiceStack,代码行数:42,代码来源:RazorPageResolver.cs


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