Modify ViewComponent_CannotFindComponent error message (#6263)

This commit is contained in:
Jass Bagga 2017-05-12 14:15:29 -07:00 committed by GitHub
parent 5eabae55cb
commit b758a86a38
5 changed files with 23 additions and 10 deletions

View File

@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
=> string.Format(CultureInfo.CurrentCulture, GetString("ViewComponent_SyncMethod_ShouldReturnValue"), p0, p1);
/// <summary>
/// A view component named '{0}' could not be found.
/// A view component named '{0}' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with '{1}' or have a class name ending with the '{2}' suffix. A view component must not be decorated with '{3}'.
/// </summary>
internal static string ViewComponent_CannotFindComponent
{
@ -75,10 +75,10 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
}
/// <summary>
/// A view component named '{0}' could not be found.
/// A view component named '{0}' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with '{1}' or have a class name ending with the '{2}' suffix. A view component must not be decorated with '{3}'.
/// </summary>
internal static string FormatViewComponent_CannotFindComponent(object p0)
=> string.Format(CultureInfo.CurrentCulture, GetString("ViewComponent_CannotFindComponent"), p0);
internal static string FormatViewComponent_CannotFindComponent(object p0, object p1, object p2, object p3)
=> string.Format(CultureInfo.CurrentCulture, GetString("ViewComponent_CannotFindComponent"), p0, p1, p2, p3);
/// <summary>
/// An invoker could not be created for the view component '{0}'.

View File

@ -131,7 +131,7 @@
<value>Method '{0}' of view component '{1}' should be declared to return a value.</value>
</data>
<data name="ViewComponent_CannotFindComponent" xml:space="preserve">
<value>A view component named '{0}' could not be found.</value>
<value>A view component named '{0}' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with '{1}' or have a class name ending with the '{2}' suffix. A view component must not be decorated with '{3}'.</value>
</data>
<data name="ViewComponent_IViewComponentFactory_ReturnedNull" xml:space="preserve">
<value>An invoker could not be created for the view component '{0}'.</value>

View File

@ -98,7 +98,11 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents
var descriptor = _selector.SelectComponent(name);
if (descriptor == null)
{
throw new InvalidOperationException(Resources.FormatViewComponent_CannotFindComponent(name));
throw new InvalidOperationException(Resources.FormatViewComponent_CannotFindComponent(
name,
nameof(ViewComponentAttribute),
ViewComponentConventions.ViewComponentSuffix,
nameof(NonViewComponentAttribute)));
}
return InvokeCoreAsync(descriptor, arguments);
@ -129,7 +133,10 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents
}
throw new InvalidOperationException(Resources.FormatViewComponent_CannotFindComponent(
componentType.FullName));
componentType.FullName,
nameof(ViewComponentAttribute),
ViewComponentConventions.ViewComponentSuffix,
nameof(NonViewComponentAttribute)));
}
// Internal for testing

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents
{
public static class ViewComponentConventions
{
private const string ViewComponentSuffix = "ViewComponent";
public static readonly string ViewComponentSuffix = "ViewComponent";
public static string GetComponentName(TypeInfo componentType)
{

View File

@ -105,7 +105,10 @@ namespace Microsoft.AspNetCore.Mvc
public async Task ExecuteResultAsync_Throws_IfViewComponentCouldNotBeFound_ByName()
{
// Arrange
var expected = "A view component named 'Text' could not be found.";
var expected = "A view component named 'Text' could not be found. A view component must be " +
"a public non-abstract class, not contain any generic parameters, and either be decorated " +
"with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. " +
"A view component must not be decorated with 'NonViewComponentAttribute'.";
var actionContext = CreateActionContext();
@ -125,7 +128,10 @@ namespace Microsoft.AspNetCore.Mvc
public async Task ExecuteResultAsync_Throws_IfViewComponentCouldNotBeFound_ByType()
{
// Arrange
var expected = $"A view component named '{typeof(TextViewComponent).FullName}' could not be found.";
var expected = $"A view component named '{typeof(TextViewComponent).FullName}' could not be found. " +
"A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated " +
"with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. " +
"A view component must not be decorated with 'NonViewComponentAttribute'.";
var actionContext = CreateActionContext();
var services = CreateServices(diagnosticListener: null, context: actionContext.HttpContext);