English 中文(简体)
Blazor 使用.net 8 (服务器和客户端) 404 误差 cors 误差
原标题:Blazor web app using .net 8 (server & client) 404 error => cors error
I discover a strange behavior on blazor .net 8. When you try to access to a wrong address from your blazor app, you will be redirected to a 404 page. However, if you have SignalR, it will not reconnect unless you exit the browser completly. How to reproduce? Create a server and wasm app using visual studio Create a console project with signalr. Specify the signalr project dependence to your server project Add you signal connection protocol on the home component Launch the app When the home page is loaded, type a reference to your blazor app page which does not exist in the browser. You will experience the behavior specified in the intro. I can provide if necessary a sample project. Did not post any project on my git related to this yet. If you know how to resolve this behavior, please let me know. I tried to use a console app for signalr because I am using the same hub for .net maui app and blazor web app.
问题回答
For checking your issue, I am using allow all origins in your project. services.AddCors(options => options.AddPolicy("CorsPolicy", builder => { builder.AllowAnyMethod() .SetIsOriginAllowed(_ => true) .AllowAnyHeader() .AllowCredentials(); })); ... app.UseCors("CorsPolicy"); ... Then I change your Home.razor page like below and it works in my side. @page "/" @inject NavigationManager Navigation Home

404 Test Page

Click on this button to navigate to a web page which does not exist.
In the program.cs for the Blazor server project, a specific page for 404 error has been specified.
Navigate to page not referenced

@if (hub.IsConnected) {

} else {

正在连接到服务器...

}

Connection status: @hub.IsConnected

Hub messages
@foreach (var item in hub.Messages) {

- @ item;

}
@code { protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { hub.InitHub(); if (hub.hubConnection != null) { await hub.ConnectAsync(); await InvokeAsync(StateHasChanged); } } } private async Task ReceiveRandomMessage() { if (hub.IsConnected) { await hub.ReceiveRandomMessage(); await InvokeAsync(StateHasChanged); } else { Console.WriteLine("Hub is not connected yet."); } } } Test Result
I found a more global solution. the only thing is that for some type of application architecture, it could be troublesome. In the .csproj file of the client, I added the following: false Without any other type of additional code it works perfectly.




相关问题
Replacing Broken External Images With Custom Image

I m looping through an array of URL strings of images hosted at an external site. It looks something like this: def get_image_urls image_url_array.each do |image_url| puts image_tag image_url ...

WordPress 404 Errors

I moved a Wordpress website from one server to another and I am now getting 404 errors on everything but the home page. I also checked that the .htaccess file is there and the database contents. They ...

HttpWebResponse returns 404 error

How to let Httpwebresponse ignore the 404 error and continue with it? It s easier than looking for exceptions in input as it is very rare when this happens.

File extension problems with classic ASP

I am running a classic ASP website where my online users can attach files to the internal message system. But whenever they upload an attachment with more then 3 characters in the file extension, the ...

Django custom handler404 shows 404 but gives header 200

I made a custom handler404 for a authenticated Django website to avoid information leakage. def check_logged_in_404(request): """ Custom 404. Show friendly 404 when logged in and redirect to /...

Http 403 or 404 for accessing restricted WEB resource?

My question is rather similar to this Return “correct” error code, or protect privacy?, but I d like to hear some different answers. We have WEB site most pages of which may be visited by not logged ...

ErrorDocument 404 Not Sending Referrer Information: PHP

I m using a standard htaccess ErrorDocument 404 to redirect users to a new page. In order to customize the 404, I need to know their referrer information (from the page they Tried to visit). $...

热门标签