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