Added printing of route values when a route is not matched

This commit is contained in:
Joonas Westlin 2016-12-06 12:37:57 -08:00 committed by Kiran Challa
parent 17dc23a024
commit d44c9aee1e
4 changed files with 25 additions and 6 deletions

View File

@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
var actionDescriptor = _actionSelector.SelectBestCandidate(context, Actions);
if (actionDescriptor == null)
{
_logger.NoActionsMatched();
_logger.NoActionsMatched(context.RouteData.Values);
return TaskCache.CompletedTask;
}

View File

@ -67,6 +67,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
private static readonly Action<ILogger, string, string, Exception> _redirectToRouteResultExecuting;
private static readonly Action<ILogger, string[], Exception> _noActionsMatched;
static MvcCoreLoggerExtensions()
{
_actionExecuting = LoggerMessage.Define<string>(
@ -223,6 +225,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
LogLevel.Information,
1,
"Executing RedirectToRouteResult, redirecting to {Destination} from route {RouteName}.");
_noActionsMatched = LoggerMessage.Define<string[]>(
LogLevel.Debug,
3,
"No actions matched the current request. Route values: {RouteValues}");
}
public static IDisposable ActionScope(this ILogger logger, ActionDescriptor action)
@ -250,9 +257,19 @@ namespace Microsoft.AspNetCore.Mvc.Internal
}
}
public static void NoActionsMatched(this ILogger logger)
public static void NoActionsMatched(this ILogger logger, IDictionary<string, object> routeValueDictionary)
{
logger.LogDebug(3, "No actions matched the current request");
if (logger.IsEnabled(LogLevel.Debug))
{
string[] routeValues = null;
if (routeValueDictionary != null)
{
routeValues = routeValueDictionary
.Select(pair => pair.Key + "=" + Convert.ToString(pair.Value))
.ToArray();
}
_noActionsMatched(logger, routeValues, null);
}
}
public static void ChallengeResultExecuting(this ILogger logger, IList<string> schemes)

View File

@ -66,14 +66,14 @@ namespace Microsoft.AspNetCore.Mvc.Internal
var candidates = _actionSelector.SelectCandidates(context);
if (candidates == null || candidates.Count == 0)
{
_logger.NoActionsMatched();
_logger.NoActionsMatched(context.RouteData.Values);
return TaskCache.CompletedTask;
}
var actionDescriptor = _actionSelector.SelectBestCandidate(context, candidates);
if (actionDescriptor == null)
{
_logger.NoActionsMatched();
_logger.NoActionsMatched(context.RouteData.Values);
return TaskCache.CompletedTask;
}

View File

@ -30,12 +30,14 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
.Returns(new ActionDescriptor[0]);
var context = CreateRouteContext();
context.RouteData.Values.Add("controller", "Home");
context.RouteData.Values.Add("action", "Index");
var handler = CreateMvcRouteHandler(
actionSelector: mockActionSelector.Object,
loggerFactory: loggerFactory);
var expectedMessage = "No actions matched the current request";
var expectedMessage = "No actions matched the current request. Route values: controller=Home, action=Index";
// Act
await handler.RouteAsync(context);