9

I'm not finding where should I click to create a partial class in visual studio 2019.

After I create a new "Razor Component" file, I can't add the partial class to it.

Where do I add it?

Vencovsky
  • 28,550
  • 17
  • 109
  • 176
  • 1
    you do it manually, just create class with the same name and .cs extension – Alexan Mar 05 '20 at 17:53
  • 2
    Add `.razor.cs` class manually to the same folder, and make sure they both are in the same namespace (the `@namespace` directive in Razor component might be necessary). – Roger Wolf Mar 05 '20 at 20:53

3 Answers3

17

There are two ways to add code behind to Razor component:

  1. Add Base class and in component add @inherits this class, how is described in this answer. In this case base class shouldn't be partial.

  2. Since October 2019 we can use partial classes. You can just add class name with the same name, adding .cs extension, mark it as partial class:

enter image description here

And you don't need to use @inherits in this case.

See Partial class support doc.

Alexan
  • 8,165
  • 14
  • 74
  • 101
3

Something like this? : - razor page with "code-behind" class

enter image description here

yob
  • 528
  • 4
  • 9
  • 1
    if you use partial class in code behind class name should be the same as in component: Weather. And you don't need inherit in this case. – Alexan Mar 06 '20 at 00:50
2

Another way: (vs 2022)

Right click the @Code section, pick Quick Actions and Refactorings, select Extract block to code behind.

enter image description here

sergiokml
  • 41
  • 6