code section in blazor

Saeed Pooladzadeh 231 Reputation points
2022-03-13T15:31:48.003+00:00

Hello,

Can the code that is written in Blazor in the code section be written in the controller?
Are the codes exactly the same?

thanks,

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,165 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-03-14T03:22:40.02+00:00

    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:

    182651-1.gif

    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:

    182549-2.gif


    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

    2 people found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,021 Reputation points
    2022-03-13T16:11:19.367+00:00

    With blazor there is no controller code. You build razor components classes. You can include the code in the code section, or create a code behind partial class. The partial class code works the same as any partial class.

    1 person found this answer helpful.

  2. AlexChow 11 Reputation points MVP
    2022-12-07T20:53:52.987+00:00

    Not exactly the same, the way of injecting services is a little different

    Controller

        public class UploadController : Controller  
        {  
            private readonly IWebHostEnvironment environment;  
            private readonly ILogger<UploadController> _logger;  
            private readonly IConfiguration _config;  
      
            public UploadController(IWebHostEnvironment environment, ILogger<UploadController> logger, IConfiguration config)  
            {  
                this.environment = environment;  
                this._logger = logger;  
                this._config = config;  
            }  
    }  
    

    Blazor

    @inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment environment  
    @inject IConfiguration _config  
    
    
    
      
    
    0 comments No comments