借助互联网上的一些示例,我可以开发ASP.NET Core Hosted Blazor应用程序.
但是,虽然呼吁api如下
private async Task Refresh()
{
li.Clear();
li = await Http.GetJsonAsync<SampleModel[]>("/api/Sample/GetList");
StateHasChanged();
}
private async Task Save()
{
await Http.SendJsonAsync(HttpMethod.Post, "api/Sample/Add", obj);
await Refresh();
}
Run Code Online (Sandbox Code Playgroud)
在下面的行中:
await Http.SendJsonAsync(HttpMethod.Post, "api/Sample/Add", obj);
Run Code Online (Sandbox Code Playgroud)
如何查看此HTTP呼叫的状态代码?
如果API调用中出现任何问题而不是我想显示消息.
但当我这样做时:
HttpResponseMessage resp = await Http.SendJsonAsync(HttpMethod.Post, "api/Sample/Add", obj);
Run Code Online (Sandbox Code Playgroud)
然后它说:
无法将
HttpResponse消息转换为消息
我使用以下方法:
GetJsonAsync() // For HttpGet
SendJsonAsync() // For HttpPost And Put
DeleteAsync() // For HttpDelete
Run Code Online (Sandbox Code Playgroud)
如何在此验证状态代码?
c# asp.net-core-webapi blazor asp.net-core-2.1 blazor-server-side
我看到了大量用于通过服务器端Blazor应用程序启用Azure AD的文档/示例。不幸的是,Asp.net托管Blazor应用程序似乎有零(0)个文档。
至少,我想使用Azure AD保护API。但是,不幸的是,Asp.net托管Blazor项目的VS项目模板不允许任何身份验证选项。
- 这是不可能的吗?我们可以期望这样吗
- 将来支持(希望在不久的将来)?
谢谢。
azure-active-directory blazor blazor-server-side blazor-client-side
我动态创建 NavMenu 并返回菜单我用户的数据库和索引页中我已经返回了数据库中的某些内容但是当我运行应用程序或重新加载它时会显示以下错误
InvalidOperationException:在上一个操作完成之前,在此上下文中启动了第二个操作。这通常是由使用相同 DbContext 实例的不同线程引起的。有关如何避免 DbContext 线程问题的详细信息,请参阅 https://go.microsoft.com/fwlink/?linkid=2097913。
导航菜单代码,
List<Menu> menus = new List<Menu>();
protected override async Task OnInitializedAsync()
{
menus = await MenuService.GetMenus();
}
Run Code Online (Sandbox Code Playgroud)
索引代码
@if (priorities == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var priority in priorities)
{
<tr>
<td>@priority.Name</td>
</tr>
}
</tbody>
</table>
}
@code {
List<Priority> priorities;
protected override async Task OnInitializedAsync()
{
priorities = await PriorityService.GetPriorities();
}
}
Run Code Online (Sandbox Code Playgroud) 我的ChangeValues服务器端事件每 5 秒发生一次,但在客户端,我只看到number变量的第一个值。见下面的代码
@page "/"
<button class="btn btn-primary" @onclick="ChangeValues">Click me</button>
<b>@number</b>
@code {
double number;
private Random rnd = new Random();
private System.Threading.Timer _timer;
void ChangeValues()
{
number = rnd.NextDouble();
Console.WriteLine(number);
}
private void DoWork(object state)
{
ChangeValues();
}
protected override async Task OnInitializedAsync()
{
_timer = new System.Threading.Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromSeconds(5));
}
}
Run Code Online (Sandbox Code Playgroud) 如何在 Blazor 服务器端应用程序中自定义默认Register和Login页面的视图?
每次非登录用户尝试访问页面时,我都想使用标签中的NotAuthorized属性<AuthorizeRouteView>重定向到登录页面。
然而,它需要一个RenderFragment<AuthentificationState>类型化的参数。我应该设置什么来设置这个参数来呈现登录页面?
编辑:代码非常简单。我使用了身份存储在应用程序中的 Blazor 服务器端项目模板,只需添加RedirectToLogin.razor如下内容:
@inject NavigationManager NavigationManager
@code {
protected override void OnAfterRender()
{
NavigationManager.NavigateTo("counter"); //for an unknown reason, the "Identity/Account/Login" redirect doesn't work.
}
}
Run Code Online (Sandbox Code Playgroud)
并修改了App.razor:
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if(true) { } //Used for breakpoint.
<RedirectToLogin />
</NotAuthorized>
<Authorizing>
@if(true) { } //Used for breakpoint.
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState> …Run Code Online (Sandbox Code Playgroud) 我尝试在 Blazor 应用程序中实现本地化,但在尝试通过视图中的键获取资源值时遇到了一些问题。
我怎么能做到这一点?
到目前为止,这是我的代码:
启动文件
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
services.Configure<RequestLocalizationOptions>(
options =>
{
List<CultureInfo> supportedCultures =
new List<CultureInfo>
{
new CultureInfo("bg-BG"),
new CultureInfo("en-US")
};
options.DefaultRequestCulture = new RequestCulture("bg-BG");
// Formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// UI string
options.SupportedUICultures = supportedCultures;
});
services.AddRazorPages();
services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
services.AddApplicationRepositoryServices();
services.AddSingleton<WeatherForecastService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRequestLocalization( app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error"); …Run Code Online (Sandbox Code Playgroud) 假设我有一个长时间运行的任务,我已经初始化并从我的页面类的 OnInitializedAsync() 方法开始,该方法派生自 Microsoft.AspNetCore.Components.ComponentBase。我用它来收集数据,它不时更新用户界面,效果很好。
但在某些时候,我需要摆脱那个后台任务。当客户端切换到另一个页面或离开网络应用程序时,我想取消我的任务,这样它就不会一直运行下去。我没有找到合适的生命周期方法。
有什么建议?
我正在 VS2019 中从事服务器端 3.0.100 blazor 项目。
我有几页。如果在调试时我可以启动非默认路由/页面以进行调试,我会很高兴。
我在项目属性调试选项卡上尝试过的内容:
1) 设置启动浏览器的相对路径。如果我设置这个,比如说“设备列表”,应用程序从 localhost:44325/equipmentlist 开始,但会显示根路由。如果我随后导航到设备列表页面,URL 将更改为 localhost:44325/equipmentlist/equipmentlist 并显示正确的内容。
2) 设置 App URL:这与 1 的行为相同
每次单击调试时,我都想为自己节省额外的点击次数。
编辑:“启动浏览器”设置更改了浏览器启动时显示的地址,但显示的内容仍然是默认路由。
即 localhost:44325/equipmentlist/ 显示在浏览器地址栏中,但它仍然显示“/”页面中的内容。我必须导航到 localhost:44325/equipmentlist/equipmentlist 才能看到所需的内容。
我正在学习来自 WinForm UWP 背景的 Blazor。我有一个游戏列表:
public class Game
{
public string ID { get; set; }
public string Text { get; set; }
public override string ToString()
{
return Text;
}
}
List<Game> Games = new List<Game> {
new Game() { ID= "Game1", Text= "American Football"},
new Game() { ID= "Game2", Text= "Badminton" },
new Game() { ID= "Game3", Text= "Basketball" },
new Game() { ID= "Game4", Text= "Cricket"},
new Game() { ID= "Game5", Text= "Football" },
new Game() { …Run Code Online (Sandbox Code Playgroud)