本文整理汇总了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("<", "<").Replace(">", ">");
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);
}
示例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();
}
}
示例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("<", "<").Replace(">", ">");
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);
}
示例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;
}
示例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
}
示例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);
}
}
示例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();
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
示例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();
}
}