当我try 将依赖项注入与非参数构造函数一起使用时,我在Blazor服务器应用程序中遇到了‘MissingMethodException’MissingMethodException: Cannot dynamically create an instance of type 'Blazor.Pages.Account.Register'. Reason: No parameterless constructor defined
.以下是我到目前为止try 过的:
我已经创建了UserService.cs
using Blazor.API.Model;
namespace Blazor.Services
{
public class UserService
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<bool> RegisterAsync(RegisterModel registerModel)
{
try
{
var response = await _httpClient.PostAsJsonAsync("/register", registerModel);
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
}
我已经更新了Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<HttpClient>(s =>
{
var httpClient = new HttpClient
{
BaseAddress = new Uri(builder.Configuration.GetSection("ApiBaseUrl").Value)
};
return httpClient;
});
builder.Services.AddHttpClient();
builder.Services.AddScoped<UserService>();
var app = builder.Build();
另外,当我将鼠标悬停在builder.Configuration.GetSection("ApiBaseUrl").Value
时,我收到错误"Warning CS8604 Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString)'"
和我的注册表.razor.cs
using Blazor.API.Model;
using Blazor.Services;
namespace Blazor.Pages.Account
{
public partial class Register
{
private UserService UserService { get; set; }
private bool isRegistering = false;
private bool registrationSuccessful = false;
private RegisterModel registerModel = new RegisterModel();
public Register(UserService userService)
{
UserService = userService;
}
private async Task HandleValidSubmit()
{
// Start the registration process
isRegistering = true;
//send the registration request to the server
bool registrationResult = await UserService.RegisterAsync(registerModel);
// Update the registration state
registrationSuccessful = registrationResult;
// Stop the registration process
isRegistering = false;
}
}
}
但是,当我导航到此RegisterComponent时,我收到了‘MissingMethodException’错误.我也try 过添加无参数构造函数,但它导致了一个警告:‘不可为空的属性必须包含非空值’.
我如何解决这个问题,并正确使用依赖注入与Blazor组件的非参数构造函数?
这是我的登记簿.剃须刀
@page "/register"
<h3 class="text-center">Register</h3>
<div class="form-container">
<EditForm Model="@registerModel" OnValidSubmit="HandleValidSubmit" >
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="FirstName" class="form-label">First Name</label>
<InputText id="FirstName" @bind-Value="registerModel.FirstName" class="form-control" />
<ValidationMessage For="@(() => registerModel.FirstName)" />
</div>
<div class="form-group">
<label for="LastName" class="form-label">Last Name</label>
<InputText id="LastName" @bind-Value="registerModel.LastName" class="form-control" />
<ValidationMessage For="@(() => registerModel.LastName)" />
</div>
<div class="form-group">
<label for="Email" class="form-label">Email</label>
<InputText id="Email" @bind-Value="registerModel.Email" class="form-control" />
<ValidationMessage For="@(() => registerModel.Email)" />
</div>
<div class="form-group">
<label for="PhoneNumber" class="form-label">Phone Number</label>
<InputText id="PhoneNumber" @bind-Value="registerModel.PhoneNumber" class="form-control" />
<ValidationMessage For="@(() => registerModel.PhoneNumber)" />
</div>
<div class="form-group">
<label for="Password" class="form-label">Password</label>
<InputText id="Password" @bind-Value="registerModel.Password" class="form-control" type="password" />
<ValidationMessage For="@(() => registerModel.Password)" />
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary" disabled="@isRegistering">
@if (isRegistering)
{
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<text>Registering...</text>
}
else if (registrationSuccessful)
{
<text>Registration Successful</text>
}
else
{
<text>Register</text>
}
</button>
</div>
<p class="text-center mt-3">
Already have an account? <a href="/login">Login here</a>
</p>
</EditForm>
</div>
@code {
}
和我的appSetting.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ApiBaseUrl": "https://localhost:44330/"
}
我是Blazor服务器应用程序的初学者,目前我正在学习如何在Blazor服务器应用程序中连接和交互API.具体地说,我正在致力于实现用户注册和登录功能.然而,我在设置HttpClient和为注册过程建立正确的流程方面遇到了困难,如本文所述.