Blazor项目中如何在C# cs文件中使用Razor页面定义的变量?
Got it, let's break this down clearly since you already know how to reference CS file variables in Razor with @variableName. The approach depends on whether you're working with a code-behind partial class (the .razor.cs file tied directly to your Razor page) or a separate, standalone C# class (like a service or utility class).
1. 针对代码后置的Partial Class(最常见场景)
Blazor's code-behind files (e.g., MyComponent.razor.cs) are part of the same partial class as your Razor page (MyComponent.razor). The only catch is access modifiers—Razor page variables default to private, which the partial CS file can't access. Here's how to fix that:
Step 1: Define the variable in Razor with a proper access modifier
In your .razor file, declare the variable with protected, public, or internal instead of the default private:
@page "/my-component" <!-- 定义可被CS文件访问的变量 --> @protected string MyRazorVariable { get; set; } = "Hello from Razor!"; <p>Current value: @MyRazorVariable</p> <button @onclick="UpdateFromCs">Update from CS File</button>
Step 2: Access/modify the variable directly in the code-behind file
In your matching .razor.cs file, since it's the same partial class, you can reference the variable just like any other class member:
public partial class MyComponent { private void UpdateFromCs() { // 直接调用Razor里的变量 Console.WriteLine($"Current Razor variable value: {MyRazorVariable}"); // 也可以修改它,Razor页面会自动更新UI MyRazorVariable = "Value updated from code-behind!"; } }
2. 针对独立的C#类(如服务、工具类)
If you need to pass the Razor variable to a separate class (not tied to the component), you have two straightforward options:
Option A: Pass the variable as a method parameter
Call the class's method from your Razor page and pass the variable directly:
@inject MyCustomService MyService <button @onclick="() => MyService.ProcessRazorVariable(MyRazorVariable)">Send to Service</button> @code { protected string MyRazorVariable { get; set; } = "Data from Razor"; }
Then in your standalone C# class:
public class MyCustomService { public void ProcessRazorVariable(string inputValue) { // 使用传递过来的变量 Console.WriteLine($"Received from Razor: {inputValue}"); // 在这里做业务逻辑处理 } }
Option B: Assign the variable to a class property
If you need the class to hold onto the variable for later use, set a property on the class from your Razor component:
@inject MyCustomService MyService @code { protected string MyRazorVariable { get; set; } = "Persistent data"; protected override void OnInitialized() { // 将Razor变量赋值给服务类的属性 MyService.StoredValue = MyRazorVariable; base.OnInitialized(); } }
In your service class:
public class MyCustomService { public string StoredValue { get; set; } public void UseStoredValue() { // 直接使用已存储的Razor变量 Console.WriteLine($"Stored Razor value: {StoredValue}"); } }
Key Note
Always remember: private variables in Razor can't be accessed by code-behind or other classes. Stick to protected (best for component partial classes) or public/internal if you need broader access.
内容的提问来源于stack exchange,提问作者Mdarende

