當前位置: 首頁>>代碼示例>>C#>>正文


C# HttpRequestMessage.SetHttpContext方法代碼示例

本文整理匯總了C#中System.Net.Http.HttpRequestMessage.SetHttpContext方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpRequestMessage.SetHttpContext方法的具體用法?C# HttpRequestMessage.SetHttpContext怎麽用?C# HttpRequestMessage.SetHttpContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.Http.HttpRequestMessage的用法示例。


在下文中一共展示了HttpRequestMessage.SetHttpContext方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ConvertRequest

        internal static HttpRequestMessage ConvertRequest(HttpContextBase httpContextBase)
        {
            Contract.Assert(httpContextBase != null);

            HttpRequestBase requestBase = httpContextBase.Request;
            HttpMethod method = HttpMethodHelper.GetHttpMethod(requestBase.HttpMethod);
            Uri uri = requestBase.Url;
            HttpRequestMessage request = new HttpRequestMessage(method, uri);

            // Choose a buffered or bufferless input stream based on user's policy
            IHostBufferPolicySelector policySelector = _bufferPolicySelector.Value;
            bool isInputBuffered = policySelector == null ? true : policySelector.UseBufferedInputStream(httpContextBase);

            if (isInputBuffered)
            {
                request.Content = new LazyStreamContent(() => new SeekableBufferedRequestStream(requestBase));
            }
            else
            {
                request.Content = new LazyStreamContent(() => requestBase.GetBufferlessInputStream());
            }

            foreach (string headerName in requestBase.Headers)
            {
                string[] values = requestBase.Headers.GetValues(headerName);
                AddHeaderToHttpRequestMessage(request, headerName, values);
            }

            // Add context to enable route lookup later on
            request.SetHttpContext(httpContextBase);

            HttpRequestContext requestContext = new WebHostHttpRequestContext(httpContextBase, requestBase, request);
            request.SetRequestContext(requestContext);

            IDictionary httpContextItems = httpContextBase.Items;

            // Add the OWIN environment, when available (such as when using the OWIN integrated pipeline HTTP module).
            if (httpContextItems != null && httpContextItems.Contains(OwinEnvironmentHttpContextKey))
            {
                request.Properties.Add(OwinEnvironmentKey, httpContextItems[OwinEnvironmentHttpContextKey]);
            }

            // The following three properties are set for backwards compatibility only. The request context controls
            // the behavior for all cases except when accessing the property directly by key.

            // Add the retrieve client certificate delegate to the property bag to enable lookup later on
            request.Properties.Add(HttpPropertyKeys.RetrieveClientCertificateDelegateKey, _retrieveClientCertificate);

            // Add information about whether the request is local or not
            request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy<bool>(() => requestBase.IsLocal));

            // Add information about whether custom errors are enabled for this request or not
            request.Properties.Add(HttpPropertyKeys.IncludeErrorDetailKey, new Lazy<bool>(() => !httpContextBase.IsCustomErrorEnabled));

            return request;
        }
開發者ID:quentez,項目名稱:aspnetwebstack,代碼行數:56,代碼來源:HttpControllerHandler.cs

示例2: CreateHttpRequestMessageWithContext

        private static HttpRequestMessage CreateHttpRequestMessageWithContext()
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/controllerName/actionName");
            request.SetConfiguration(new HttpConfiguration());
            request.SetHttpContext(CreateHttpContext("~/api"));

            return request;
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:8,代碼來源:HostedHttpRouteCollectionTest.cs

示例3: GetVirtualPath_ReturnsVirtualPathData

        public void GetVirtualPath_ReturnsVirtualPathData()
        {
            var request = new HttpRequestMessage();
            request.SetHttpContext(CreateHttpContext("~/api", "APP PATH MODIFIER RETURN VALUE"));
            var config = new HttpConfiguration(_webApiRoutes);
            IHttpRoute route = _webApiRoutes.CreateRoute("api", null, null);
            _webApiRoutes.Add("default", route);
            request.Properties[HttpPropertyKeys.HttpRouteDataKey] = _webApiRoutes.GetRouteData(request);
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            IHttpVirtualPathData result = _webApiRoutes.GetVirtualPath(request, null, new HttpRouteValueDictionary { { "httproute", true } });

            Assert.NotNull(result);
            Assert.Same(route, result.Route);
            Assert.Equal("APP PATH MODIFIER RETURN VALUE", result.VirtualPath);
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:16,代碼來源:HostedHttpRouteCollectionTest.cs

示例4: GetRouteData_WithHttpContext_MatchedRoute_ReturnsRouteData

        public void GetRouteData_WithHttpContext_MatchedRoute_ReturnsRouteData()
        {
            var request = new HttpRequestMessage();
            request.SetHttpContext(CreateHttpContext("~/api"));
            IHttpRoute route = _webApiRoutes.CreateRoute("api", null, null);
            _webApiRoutes.Add("default", route);

            IHttpRouteData result = _webApiRoutes.GetRouteData(request);

            Assert.Same(route, result.Route);
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:11,代碼來源:HostedHttpRouteCollectionTest.cs

示例5: ConvertRequest

        internal static HttpRequestMessage ConvertRequest(HttpContextBase httpContextBase)
        {
            Contract.Assert(httpContextBase != null);

            HttpRequestBase requestBase = httpContextBase.Request;
            HttpMethod method = HttpMethodHelper.GetHttpMethod(requestBase.HttpMethod);
            Uri uri = requestBase.Url;
            HttpRequestMessage request = new HttpRequestMessage(method, uri);

            // Choose a buffered or bufferless input stream based on user's policy
            IHostBufferPolicySelector policySelector = _bufferPolicySelector.Value;
            bool isInputBuffered = policySelector == null ? true : policySelector.UseBufferedInputStream(httpContextBase);
            Stream inputStream = isInputBuffered
                                    ? requestBase.InputStream
                                    : httpContextBase.ApplicationInstance.Request.GetBufferlessInputStream();

            request.Content = new StreamContent(inputStream);
            foreach (string headerName in requestBase.Headers)
            {
                string[] values = requestBase.Headers.GetValues(headerName);
                AddHeaderToHttpRequestMessage(request, headerName, values);
            }

            // Add context to enable route lookup later on
            request.SetHttpContext(httpContextBase);

            IDictionary httpContextItems = httpContextBase.Items;

            // Add the OWIN environment, when available (such as when using the OWIN integrated pipeline HTTP module).
            if (httpContextItems != null && httpContextItems.Contains(OwinEnvironmentHttpContextKey))
            {
                request.Properties.Add(OwinEnvironmentKey, httpContextItems[OwinEnvironmentHttpContextKey]);
            }

            // Add the retrieve client certificate delegate to the property bag to enable lookup later on
            request.Properties.Add(HttpPropertyKeys.RetrieveClientCertificateDelegateKey, _retrieveClientCertificate);

            // Add information about whether the request is local or not
            request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy<bool>(() => requestBase.IsLocal));

            // Add information about whether custom errors are enabled for this request or not
            request.Properties.Add(HttpPropertyKeys.IncludeErrorDetailKey, new Lazy<bool>(() => !httpContextBase.IsCustomErrorEnabled));

            return request;
        }
開發者ID:brianly,項目名稱:aspnetwebstack,代碼行數:45,代碼來源:HttpControllerHandler.cs

示例6: CreateRequestWithContext

 private HttpRequestMessage CreateRequestWithContext(HttpContextBase context)
 {
     HttpRequestMessage request = new HttpRequestMessage();
     request.SetHttpContext(context);
     return request;
 }
開發者ID:samgithub-duplicate,項目名稱:aspnetwebstack,代碼行數:6,代碼來源:WebHostPrincipalServiceTest.cs


注:本文中的System.Net.Http.HttpRequestMessage.SetHttpContext方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。