13

In vs2019, I added a new razor component to a working and essentially brand new Blazor App project. I renamed the file for my new razor component and noticed index.razor still referenced <oldComponentName/>, so I updated it to <newComponentName/> to match the new file name.

I see the error: Found markup element with unexpected name 'newComponentName'. If this is intended to be a component, add a @using directive for its namespace.

I built, rebuilt, and searched for a "map" of file names to component names that was out of date or something - no luck. There's no other namespace that I've (knowingly) introduced, so the @using guidance doesn't seem relevant.

At the moment, I happen to want my file names and component names to stay aligned. What am I missing here?

colbybhearn
  • 452
  • 9
  • 21
  • A Component gets its name from the file name and its namespace from the folder(s) it is in. Did you check your _imports.razor file if the `@using`s are ok? – H H Jan 18 '20 at 06:40
  • @HenkHolterman Thanks. I did check the ```@using```s in _imports.razor and they look good. – colbybhearn Jan 18 '20 at 07:24
  • 2
    I'm running into this same problem. Everything is named correctly, VS just wants to use its cached data (or something). Restarting VS and cleaning the bin/obj directories worked for me. – Dan Friedman Jan 21 '20 at 21:20

4 Answers4

22

I had to restart my Visual Studio. No additional fixes were required. The problem seems to be some cached data.

Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
1

Checklist:

  1. Your component name should starts with a capital letter, as Counter and not counter.

  2. The file name without extension is the name of your component

Thus, if you name the file name Counter.razor, your component name is Counter,

and it should be used like this :<Counter />

The error you received may also be attributed to failing to import the namespace where your component resides (in case you've defined it in a new folder you added to your app).

enet
  • 41,195
  • 5
  • 76
  • 113
  • Thanks. This was helpful in that it gave me confidence to try again. Though your items 1 and 2 checked out ok, I still had the same error. I rebuilt and it persisted. I gave VS2019 a while to think, and the error disappeared. Patience may have been the real key here. – colbybhearn Jan 18 '20 at 07:33
  • 1
    Yes, I'm well aware of this issue with VS2019, and as you say " Patience may have been the real key here" – enet Jan 18 '20 at 08:00
  • 4
    In my case I needed to close and re-open VS and the issue went away. – azpc Mar 24 '20 at 17:27
  • Done these things but the error persisted, had to restart VS. – Arad Alvand Sep 11 '20 at 01:13
1

Restarting VS often works. When it doesn't I've found opening the component and resaving (maybe with a superficial edit) can resolve

mrDavid
  • 47
  • 7
0

This problem still exists!

I also got the message advising to use an @using directive. I had also tried closing the Solution, do Cleans, Builds and Rebuilds to no avail. Restarting didn't work for me, however, in fact having exited and re-entered VS neither component was being recognised.

I decided to add a new component from the Add template with the original component name and copy over the contents of the renamed file, at which point IntelliSense decided to recognise both versions, and things then ran as expected.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Chris D
  • 9
  • 1