I'm working with ASP.NET Core 8.0.8. My razor page application has a dynamic Table.cshtml
page with route @page "/table/{table}"
that can take any model
and display the associated database information in a searchable, filterable, and sortable table. This Table.cshtml
page also shows the associated actions available to the user based on the model
. I have a TableModel.cs
class which handles all the base OnGet
and OnPost
requests but certain models will extend this class to include specific OnPost
handlers (OnPostCreate
, OnPostUpdate
, OnPostDelete
, etc...) or set different [Authorize(Roles = ...)]
roles. This is where my problem lies. I just don't know of a way to attach these derived TableModel
classes to my Table.cshtml
page without creating a separate/duplicate page for each. I have numerous models so I would really prefer to keep the rendering and base logic on one page.
For example:
/table/products
- Accessible to everyone, uses classTableModel.cs
/table/items
- Accessible to authenticated users, uses classItemsTableModel.cs
/table/users
- Accessible to admins, uses classUsersTableModel.cs
I tried to manually set the page routing in Program.cs
but to no avail.
options.Conventions.AddPageRouteModelConvention("/table/items", model => { new MyApp.Pages.ItemsTableModel(builder.Configuration); }); options.Conventions.AddPageRouteModelConvention("/table/users", model => { new MyApp.Pages.UsersTableModel(builder.Configuration); });
I also tried using partial views but ran into trouble with the needed IConfiguration
. I know setting the derived class as a property is also an option but I'm not sure how that would then work with the specific OnPost
handlers. Open to any suggestions (if this is even possible).