Log view discovery

This commit is contained in:
Kiran Challa 2015-04-09 15:50:31 -07:00
parent 18efefd5cf
commit e7728dde3f
4 changed files with 68 additions and 13 deletions

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
@ -50,10 +51,21 @@ namespace Microsoft.AspNet.Mvc
var viewEngine = ViewEngine ??
context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>();
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<PartialViewResult>>();
var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = viewEngine.FindPartialView(context, viewName)
.EnsureSuccessful()
.View;
var viewEngineResult = viewEngine.FindPartialView(context, viewName);
if (!viewEngineResult.Success)
{
logger.LogError(
"The partial view '{PartialViewName}' was not found. Searched locations: {SearchedViewLocations}",
viewName,
viewEngineResult.SearchedLocations);
}
var view = viewEngineResult.EnsureSuccessful().View;
logger.LogVerbose("The partial view '{PartialViewName}' was found.", viewName);
if (StatusCode != null)
{

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
@ -50,10 +51,21 @@ namespace Microsoft.AspNet.Mvc
var viewEngine = ViewEngine ??
context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>();
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<ViewResult>>();
var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = viewEngine.FindView(context, viewName)
.EnsureSuccessful()
.View;
var viewEngineResult = viewEngine.FindView(context, viewName);
if(!viewEngineResult.Success)
{
logger.LogError(
"The view '{ViewName}' was not found. Searched locations: {SearchedViewLocations}",
viewName,
viewEngineResult.SearchedLocations);
}
var view = viewEngineResult.EnsureSuccessful().View;
logger.LogVerbose("The view '{ViewName}' was found.", viewName);
if (StatusCode != null)
{

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Logging;
using Moq;
using Xunit;
@ -21,7 +22,7 @@ namespace Microsoft.AspNet.Mvc
"The view 'MyView' was not found. The following locations were searched:",
"Location1",
"Location2.");
var actionContext = new ActionContext(new DefaultHttpContext(),
var actionContext = new ActionContext(GetHttpContext(),
new RouteData(),
new ActionDescriptor());
var viewEngine = new Mock<IViewEngine>();
@ -47,7 +48,7 @@ namespace Microsoft.AspNet.Mvc
{
// Arrange
var viewName = "myview";
var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
var viewEngine = new Mock<IViewEngine>();
var view = Mock.Of<IView>();
@ -73,7 +74,7 @@ namespace Microsoft.AspNet.Mvc
{
// Arrange
var viewName = "some-view-name";
var context = new ActionContext(new DefaultHttpContext(),
var context = new ActionContext(GetHttpContext(),
new RouteData(),
new ActionDescriptor { Name = viewName });
var viewEngine = new Mock<ICompositeViewEngine>();
@ -109,6 +110,8 @@ namespace Microsoft.AspNet.Mvc
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
.Returns(viewEngine.Object);
serviceProvider.Setup(p => p.GetService(typeof(ILogger<PartialViewResult>)))
.Returns(new Mock<ILogger<PartialViewResult>>().Object);
context.HttpContext.RequestServices = serviceProvider.Object;
var viewResult = new PartialViewResult
@ -122,5 +125,17 @@ namespace Microsoft.AspNet.Mvc
// Assert
viewEngine.Verify();
}
private HttpContext GetHttpContext()
{
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(s => s.GetService(typeof(ILogger<PartialViewResult>)))
.Returns(new Mock<ILogger<PartialViewResult>>().Object);
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = serviceProvider.Object;
return httpContext;
}
}
}

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Logging;
using Moq;
using Xunit;
@ -21,7 +22,8 @@ namespace Microsoft.AspNet.Mvc
"The view 'MyView' was not found. The following locations were searched:",
"Location1",
"Location2.");
var actionContext = new ActionContext(new DefaultHttpContext(),
var actionContext = new ActionContext(GetHttpContext(),
new RouteData(),
new ActionDescriptor());
var viewEngine = new Mock<IViewEngine>();
@ -47,7 +49,7 @@ namespace Microsoft.AspNet.Mvc
{
// Arrange
var viewName = "myview";
var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
var viewEngine = new Mock<IViewEngine>();
var view = Mock.Of<IView>();
@ -73,7 +75,7 @@ namespace Microsoft.AspNet.Mvc
{
// Arrange
var viewName = "some-view-name";
var context = new ActionContext(new DefaultHttpContext(),
var context = new ActionContext(GetHttpContext(),
new RouteData(),
new ActionDescriptor { Name = viewName });
var viewEngine = new Mock<ICompositeViewEngine>();
@ -94,7 +96,7 @@ namespace Microsoft.AspNet.Mvc
}
[Fact]
public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()
public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()
{
// Arrange
var viewName = "some-view-name";
@ -109,6 +111,8 @@ namespace Microsoft.AspNet.Mvc
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
.Returns(viewEngine.Object);
serviceProvider.Setup(p => p.GetService(typeof(ILogger<ViewResult>)))
.Returns(new Mock<ILogger<ViewResult>>().Object);
context.HttpContext.RequestServices = serviceProvider.Object;
var viewResult = new ViewResult
@ -122,5 +126,17 @@ namespace Microsoft.AspNet.Mvc
// Assert
viewEngine.Verify();
}
private HttpContext GetHttpContext()
{
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(s => s.GetService(typeof(ILogger<ViewResult>)))
.Returns(new Mock<ILogger<ViewResult>>().Object);
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = serviceProvider.Object;
return httpContext;
}
}
}