Snake Eyes
Snake Eyes

Reputation: 16764

Blazor page Partial declarations of 'type' must not specify different base classes

I have razor page with code behind

public partial class MyRazorPage: ComponentBase, IDisposable
{
   ...
}

I have several pages that have some common stuff, a lot of common stuff.

I created an abstract class like:

public abstract class MycustomComponentBase<TItem1, TItem2> : ComponentBase where TItem1: class, new() where TItem2: class, new()
{
  ...
}

and change razor class page to

public partial class MyRazorPage: MycustomComponentBase<MyClass1, MyClass2>, IDisposable
{
   ...
}

won't work because I get:

Error CS0263 Partial declarations of 'MyRazorPage' must not specify different base classes in MyRazorPage.razor.cs

I tried public abstract partial class MycustomComponentBase<TItem1, TItem2> as well but same error.

There's way to do that on partial class? I don't want to use @inherits because authentication and custom routing is working on actual implementation.

Upvotes: 0

Views: 1258

Answers (1)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30167

Your MyRazorPage.razor and MyRazorPage.razor.cs must inherit from the same class. They both get compiled by the Razor compiler into a single class. If you don't specify @inherits in the Razor file, the Razor compiler tries to use ComponentBase which contradicts the inheritance in the partial class. Hence the error.

I don't want to use @inherits because authentication and custom routing is working on actual implementation

Solve that problem.

Upvotes: 5

Related Questions