Hi @Saeed Pooladzadeh ,
https://drive.google.com/file/d/16alMdwjMD-jzahufgMK9G14HxvDybNCH/view?usp=sharing
But you can add the controller.
From the sharing image, it seems that you are create a Blazor Server application, right? If that is the case, we can add the controller in the Blazor Server application, after add the MVC relates service and configure the route template, then we can create controller and view and use them in the Blazor application. Like this:
But as we all know, the Blazor Server apps are built using Razor components, a component is a self-contained portion of user interface (UI) with processing logic to enable dynamic behavior. So, in the component we need to use the @code
blocks/section or a code behind partial class to initialize the property and field, get the parameter values from arguments passed by parent components and route parameters and do the user event handling, lifecycle events, and custom component logic. More detail information, see ASP.NET Core Razor components. So, we can't put all @code
section content in the controller.
The reason why you want to put the @code
section to the controller, I think might there have lots of logic code to handle the data, if that is the case, I you can put them in the controller, then in the Blazor component call the controller to get the handled data and then directly display them. But, by using this method, you might need to send another http request. So, I prefer to create some service to handle the data. Then, inject service in the component. Refer this article: ASP.NET Core Blazor dependency injection.
Then, you can refer this screenshot to create a code behind partial class to store the @code
section:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion