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


C# Routing.RouteContext類代碼示例

本文整理匯總了C#中Microsoft.AspNet.Routing.RouteContext的典型用法代碼示例。如果您正苦於以下問題:C# RouteContext類的具體用法?C# RouteContext怎麽用?C# RouteContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


RouteContext類屬於Microsoft.AspNet.Routing命名空間,在下文中一共展示了RouteContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Activate_SetsPropertiesFromActionContextHierarchy

        public void Activate_SetsPropertiesFromActionContextHierarchy()
        {
            // Arrange
            var httpRequest = Mock.Of<HttpRequest>();
            var httpContext = new Mock<HttpContext>();
            httpContext.SetupGet(c => c.Request)
                       .Returns(httpRequest);
            httpContext.SetupGet(c => c.RequestServices)
                       .Returns(Mock.Of<IServiceProvider>());
            var routeContext = new RouteContext(httpContext.Object);
            var controller = new TestController();
            var context = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(context, controller.ActionContext);
            Assert.Same(httpContext.Object, controller.HttpContext);
            Assert.Same(httpRequest, controller.GetHttpRequest());
        }
開發者ID:Nakro,項目名稱:Mvc,代碼行數:25,代碼來源:DefaultControllerActivatorTest.cs

示例2: GetFormInfo

        public void GetFormInfo()
        {
            var protector = new Mock<IDataProtector>();
            var protectorProvider = new Mock<IDataProtectionProvider>();
            protectorProvider.Setup(x => x.CreateProtector(It.IsAny<string>())).Returns(protector.Object);
            var surfaceRouteParams = $"{SurfaceFormHelper.ReservedAdditionalKeys.Controller}=mycontroller&{SurfaceFormHelper.ReservedAdditionalKeys.Action}=myaction&{SurfaceFormHelper.ReservedAdditionalKeys.Area}=myarea";
            protector.Setup(dataProtector => dataProtector.Protect(It.IsAny<byte[]>())).Returns(Encoding.UTF8.GetBytes(surfaceRouteParams));
            protector.Setup(dataProtector => dataProtector.Unprotect(It.IsAny<byte[]>())).Returns(Encoding.UTF8.GetBytes(surfaceRouteParams));
            var helper = new SurfaceFormHelper(Mock.Of<ILoggerFactory>(), protectorProvider.Object);
            var request = new Mock<HttpRequest>();
            request.Setup(httpRequest => httpRequest.Method).Returns("GET");
            request.Setup(httpRequest => httpRequest.Query).Returns(
                new FormCollection(new Dictionary<string, StringValues>
                {
                    {"ufprt", Convert.ToBase64String(Encoding.UTF8.GetBytes(surfaceRouteParams))}
                }));

            var httpCtxAccessor = new Mock<IHttpContextAccessor>();
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(context => context.Request).Returns(request.Object);
            httpCtxAccessor.Setup(accessor => accessor.HttpContext).Returns(httpContext.Object);
            var routeCtx = new RouteContext(httpContext.Object);

            var result = helper.GetFormInfo(routeCtx);

            Assert.Equal("mycontroller", result.ControllerName);
            Assert.Equal("myaction", result.ActionName);
            Assert.Equal("myarea", result.Area);
        }
開發者ID:vnbaaij,項目名稱:Umbraco9,代碼行數:29,代碼來源:SurfaceFormHelperTests.cs

示例3: Activate_PopulatesServicesFromServiceContainer

        public void Activate_PopulatesServicesFromServiceContainer()
        {
            // Arrange
            var urlHelper = Mock.Of<IUrlHelper>();
            var service = new Mock<IServiceProvider>();
            service.Setup(s => s.GetService(typeof(IUrlHelper)))
                   .Returns(urlHelper);

            var httpContext = new Mock<HttpContext>();
            httpContext.SetupGet(c => c.RequestServices)
                       .Returns(service.Object);
            var routeContext = new RouteContext(httpContext.Object);
            var controller = new TestController();
            var context = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(urlHelper, controller.Helper);
        }
開發者ID:Nakro,項目名稱:Mvc,代碼行數:25,代碼來源:DefaultControllerActivatorTest.cs

示例4: RouteAsync

        public Task RouteAsync(RouteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            EnsureServices(context.HttpContext);

            var actionDescriptor = _actionSelector.Select(context);
            if (actionDescriptor == null)
            {
                _logger.NoActionsMatched();
                return TaskCache.CompletedTask;
            }

            if (actionDescriptor.RouteValueDefaults != null)
            {
                foreach (var kvp in actionDescriptor.RouteValueDefaults)
                {
                    if (!context.RouteData.Values.ContainsKey(kvp.Key))
                    {
                        context.RouteData.Values.Add(kvp.Key, kvp.Value);
                    }
                }

                // Removing RouteGroup from RouteValues to simulate the result of conventional routing
                context.RouteData.Values.Remove(TreeRouter.RouteGroupKey);
            }

            context.Handler = (c) => InvokeActionAsync(c, actionDescriptor);
            return TaskCache.CompletedTask;
        }
開發者ID:phinq19,項目名稱:git_example,代碼行數:33,代碼來源:MvcRouteHandler.cs

示例5: RouteAsync

        public async Task RouteAsync(RouteContext context) {
            EnsureLoggers(context.HttpContext);
            using(_logger.BeginScope("SubdomainTemplateRoute.RouteAsync")) {
                var host = context.HttpContext.Request.Host.Value;

                if(host.Contains(":")) {
                    host = host.Substring(0, host.IndexOf(":"));
                }

                var values = _matcher.Match(host);
                if(values == null) {
                    _logger.Log(x => x.Verbose, x => x.LogVerbose(nameof(SubdomainTemplateRoute) + " " + Name + " - Host \"" + context.HttpContext.Request.Host + "\" did not match."));
                    return;
                }

                var oldRouteData = context.RouteData;
                var newRouteData = new RouteData(oldRouteData);

                newRouteData.DataTokens.MergeValues(DataTokens);
                newRouteData.Routers.Add(_target);
                newRouteData.Values.MergeValues(values);

                try {
                    context.RouteData = newRouteData;
                    await _innerRoute.RouteAsync(context);
                }
                finally {
                    if(!context.IsHandled) {
                        context.RouteData = oldRouteData;
                    }
                }
            }
        }
開發者ID:migrap,項目名稱:Migrap.AspNet.Routing,代碼行數:33,代碼來源:SubdomainTemplateRoute.cs

示例6: RouteAsync

        public async Task RouteAsync(RouteContext context)
        {
            if (IsValidRequest(context.HttpContext))
            {
                try
                {
                    // Store the requested targetted action so that the OrchardMiddleware
                    // can continue with it once the tenant pipeline has been executed

                    if (_pipeline != null)
                    {
                        context.HttpContext.Items["orchard.Handler.Target"] = _target;
                        context.HttpContext.Items["orchard.Handler.RouteContext"] = context;

                        await _pipeline.Invoke(context.HttpContext);
                    }
                    else
                    {
                        await _target.RouteAsync(context);
                    }
                }
                catch (Exception ex)
                {
                    var logger = context.HttpContext.ApplicationServices.GetService<ILogger<TenantRoute>>();
                    logger.LogError("Error occured serving tenant route", ex);
                    throw;
                }
            }
        }
開發者ID:adwardliu,項目名稱:Orchard2,代碼行數:29,代碼來源:TenantRoute.cs

示例7: Invoke

        public async Task Invoke(HttpContext httpContext)
        {
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation("Begin Routing Request");
            }

            var shellSettings = httpContext.RequestServices.GetService<ShellSettings>();
            var routerTable = httpContext.ApplicationServices.GetService<IRunningShellRouterTable>();

            var router = routerTable.GetOrAdd(
                shellSettings.Name, 
                name => httpContext.RequestServices.GetService<IRouteBuilder>().Build()
            );

            var context = new RouteContext(httpContext);
            context.RouteData.Routers.Add(router);

            await router.RouteAsync(context);

            if (!context.IsHandled)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Request did not match any routes.");
                }

                await _next.Invoke(httpContext);
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation("End Routing Request");
            }
        }
開發者ID:mzgnr,項目名稱:Orchard2,代碼行數:35,代碼來源:OrchardRouterMiddleware.cs

示例8: RouteAsync

        public async Task RouteAsync(RouteContext context)
        {
            // Saving and restoring the original route data ensures that any values we
            // add won't 'leak' if action selection doesn't match.
            var oldRouteData = context.RouteData;

            // For diagnostics and link-generation purposes, routing should include
            // a list of IRoute instances that lead to the ultimate destination.
            // It's the responsibility of each IRouter to add the 'next' before 
            // calling it.
            var newRouteData = new RouteData(oldRouteData);
            newRouteData.Routers.Add(_next);

            var locale = GetLocale(context.HttpContext) ?? "en-US";
            newRouteData.Values.Add("locale", locale);

            try
            {
                context.RouteData = newRouteData;
                await _next.RouteAsync(context);
            }
            finally
            {
                if (!context.IsHandled)
                {
                    context.RouteData = oldRouteData;
                }
            }
        }
開發者ID:AndersBillLinden,項目名稱:Mvc,代碼行數:29,代碼來源:LocalizedRoute.cs

示例9: RouteAsync

        public async Task RouteAsync(RouteContext context)
        {
            var request = context.HttpContext.Request;

            Uri uri;
            PathString remaining;
            if (!request.Path.StartsWithSegments(PathString.FromUriComponent("/" + _routePrefix), out remaining))
            {
                // Fallback to other routes.
                return;
            }

            uri = new Uri(remaining.ToString(), UriKind.Relative);
            
            context.HttpContext.ODataProperties().Model = _model;
            var parser = new ODataUriParser(_model, uri);
            var path = parser.ParsePath();
            context.HttpContext.ODataProperties().NewPath = path;
            context.HttpContext.ODataProperties().Path =
                context.HttpContext.ODataPathHandler().Parse(_model, "http://service-root/", remaining.ToString());
            context.HttpContext.ODataProperties().IsValidODataRequest = true;

            await m.RouteAsync(context);
            context.IsHandled = true;
        }
開發者ID:emreolgun,項目名稱:WebApi,代碼行數:25,代碼來源:ODataRoute.cs

示例10: RouteAsync

        public async virtual Task RouteAsync(RouteContext context)
        {
            for (var i = 0; i < Count; i++)
            {
                var route = this[i];

                var oldRouteData = context.RouteData;

                var newRouteData = new RouteData(oldRouteData);
                newRouteData.Routers.Add(route);

                try
                {
                    context.RouteData = newRouteData;

                    await route.RouteAsync(context);
                    if (context.IsHandled)
                    {
                        break;
                    }
                }
                finally
                {
                    if (!context.IsHandled)
                    {
                        context.RouteData = oldRouteData;
                    }
                }
            }
        }
開發者ID:TerabyteX,項目名稱:Routing,代碼行數:30,代碼來源:RouteCollection.cs

示例11: RouteAsync

		public async Task RouteAsync(RouteContext context)
		{
			context.RouteData.Values["controller"] = context.RouteData.Values["controller"].ToString().Replace("-", "_");

			context.RouteData.Values["action"] = context.RouteData.Values["action"].ToString().Replace("-", "_");

			await _target.RouteAsync(context);
		}
開發者ID:al-main,項目名稱:ui-for-aspnet-mvc-6-demos,代碼行數:8,代碼來源:HyphenatedRouteHandler.cs

示例12: RouteAsync

        public async Task RouteAsync(RouteContext context) {
            if (context.HttpContext.Request.Host.Value == _urlHost) {
                context.HttpContext.Items["orchard.Handler"] = new Func<Task>(async () => {
                    await _target.RouteAsync(context);
                });

                await _pipeline.Invoke(context.HttpContext);
            }
        }
開發者ID:jefth,項目名稱:OrchardNoCMS,代碼行數:9,代碼來源:TenantRoute.cs

示例13: RouteAsync

        public async Task RouteAsync(RouteContext context)
        {
            context.RouteData.Routers.Add(_next);

            var locale = GetLocale(context.HttpContext) ?? "en-US";
            context.RouteData.Values.Add("locale", locale);

            await _next.RouteAsync(context);
        }
開發者ID:phinq19,項目名稱:git_example,代碼行數:9,代碼來源:LocalizedRoute.cs

示例14: SelectAsync

        public async Task<ActionDescriptor> SelectAsync(RouteContext context)
        {
            if (context.HttpContext.ODataProperties().IsValidODataRequest)
            {
                return await Task.FromResult(_convention.SelectAction(context));
            }

            return await _selector.SelectAsync(context);
        }
開發者ID:ZhaoYngTest01,項目名稱:WebApi,代碼行數:9,代碼來源:ODataActionSelector.cs

示例15: RouteAsync

 public async Task RouteAsync(RouteContext context)
 {
     var name = context.RouteData.Values["name"] as string;
     if (String.IsNullOrEmpty(name))
     {
         return;
     }
     await context.HttpContext.Response.WriteAsync($"Hi {name}!");
     context.IsHandled = true;
 }
開發者ID:ChujianA,項目名稱:aspnetcore-doc-cn,代碼行數:10,代碼來源:HelloRouter.cs


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