Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Update error for "generic ViewComponents" to explain what a valid view component is #5380

Closed
NTaylorMullen opened this issue Oct 7, 2016 · 6 comments
Assignees
Labels
3 - Done bug up-for-grabs Members of our awesome commnity can handle this issue

Comments

@NTaylorMullen
Copy link
Member

Given the following example:

public class FooViewComponent<T>
{
    public string Invoke(T value)
    {
        return value.ToString();
    }
}

Trying to run this like so:

@{
    Write(await Component.InvokeAsync<FooViewComponent<string>>(new
    {
        value = "Hello World"
    }));
}

Results in the following:
image

@NTaylorMullen
Copy link
Member Author

I'm not entirely sure if we want to support generic ViewComponents due to how you can invoke a ViewComponent. IE await Component.InvokeAsync("VC Name", params...).

At worst we should at least create a more readable error.

@Eilon Eilon changed the title ViewComponentHelper does not work with generic ViewComponents. Update error for "generic ViewComponents" to explain what a valid view component is Oct 7, 2016
@Eilon
Copy link
Member

Eilon commented Oct 7, 2016

After discussion with @NTaylorMullen @dougbu and @rynowak we decided to just update the error message to be more like:

A view component named '<type>' could not be found. View components must be X, Y, and Z.

Where X, Y, and Z are an indication of the criteria for being a view component, e.g.:

  • Public non-generic concrete type
  • Implement IFoo interface
  • Whatever else

(I don't recall the exact rules.)

@Eilon Eilon added bug 1 - Ready up-for-grabs Members of our awesome commnity can handle this issue labels Oct 7, 2016
@Eilon Eilon added this to the 1.1.0 milestone Oct 7, 2016
@iscifoni
Copy link
Contributor

iscifoni commented Oct 11, 2016

Hi,

from the code ( https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs#L76 ), I think the rules is :

  • is class
  • is public
  • not is abstract
  • not contains generic parameter
  • not is defined NonViewComponentAttribute
  • is defined ViewComponentAttribute
  • class name end with "ViewComponent"

So the message could be :

"A view component named '<type>' could not be found. View components must be a Public non-generic concrete type, not contains generic parameters, use ViewComponentAttribute or class name end with 'ViewComponent' ".

@Eilon
Copy link
Member

Eilon commented May 10, 2017

@jbagga - can you look at #5598 and #5492, which are both potential fixes for this error message, and come up with a proposed final message so that we can review the grammar/style and get this merged?

@jbagga
Copy link
Contributor

jbagga commented May 12, 2017

@Eilon thoughts on this:
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 the '{1}' or have a class name ending with the '{2}' suffix. A view component must not be decorated with the '{3}'.

Where {0}: nameof(MyViewComponent)
{1}: nameof(ViewComponentAttribute)
{2}: ViewComponentConventions.ViewComponentSuffix
{3}: nameof(NonViewComponentAttribute)

@Eilon
Copy link
Member

Eilon commented May 12, 2017

Looks great! For (1) and (3) I would just change it to say ... the {1/3} **attribute**...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 - Done bug up-for-grabs Members of our awesome commnity can handle this issue
Projects
None yet
Development

No branches or pull requests

5 participants