简体   繁体   中英

POST 405 (Method Not Allowed) error on Azure using Blazor, but everything works on localhost

I just finished my little project with .NET 6 , Blazor on Visual Studio 2022 , and everything works just fine on localhost , but when I published it on Azure , and I try to register again on my app, I'm receiving this error in the console:

POST https://domasjonaitis.azurewebsites.net/api/auth/register 405 (Method Not Allowed)

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: ExpectedStartOfValueNotFound, T Path: $ | LineNumber: 0 | BytePositionInLine: 0.
System.Text.Json.JsonException: ExpectedStartOfValueNotFound, T Path: $ | LineNumber: 0 | BytePositionInLine: 0.
 ---> System.Text.Json.JsonReaderException: ExpectedStartOfValueNotFound, T LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& , ExceptionResource , Byte , ReadOnlySpan`1 )
   at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte )
   at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte )
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1[[BlazorApp.Shared.ServiceResponse`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], BlazorApp.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader& , JsonSerializerOptions , ReadStack& )
   Exception_EndOfInnerExceptionStack
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& , JsonReaderException )
   at System.Text.Json.Serialization.JsonConverter`1[[BlazorApp.Shared.ServiceResponse`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], BlazorApp.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader& , JsonSerializerOptions , ReadStack& )
   at System.Text.Json.JsonSerializer.ReadCore[ServiceResponse`1](JsonConverter , Utf8JsonReader& , JsonSerializerOptions , ReadStack& )
   at System.Text.Json.JsonSerializer.ReadCore[ServiceResponse`1](JsonReaderState& , Boolean , ReadOnlySpan`1 , JsonSerializerOptions , ReadStack& , JsonConverter )
   at System.Text.Json.JsonSerializer.ContinueDeserialize[ServiceResponse`1](ReadBufferState& , JsonReaderState& , ReadStack& , JsonConverter , JsonSerializerOptions )
   at System.Text.Json.JsonSerializer.<ReadAllAsync>d__65`1[[BlazorApp.Shared.ServiceResponse`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], BlazorApp.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at System.Net.Http.Json.HttpContentJsonExtensions.<ReadFromJsonAsyncCore>d__4`1[[BlazorApp.Shared.ServiceResponse`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], BlazorApp.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at BlazorApp.Client.Services.AuthService.AuthService.Register(UserRegister request)
   at BlazorApp.Client.Pages.Register.HandleRegistration()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

And the same happens if I try to login with an existing (which is user@example.com 123123).

I followed through many similar threads that I found, but none of them worked.

You can find my project with all the files included on my GitHub page: https://github.com/domasjohn/BlazorApp

And here is my azure website, that you can check out by yourself: https://domasjonaitis.azurewebsites.net/

Also, I'm a total new beginner, so a simple explanation with the detailed instructions would be appreciated

Thanks.

I could not check your website as it's stopped, but I do believe it's a CORS issue. All you have to do is add the following to your Startup.cs class:

services.AddCors(opt =>
        {
            opt.AddPolicy(name: _policyName, builder =>
            {
                builder.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
            });
        });

here's a full sample of how it will look like:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    private readonly string _policyName = "CorsPolicy";

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(opt =>
        {
            opt.AddPolicy(name: _policyName, builder =>
            {
                builder.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
            });
        });
        services.AddControllers();
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseCors(_policyName);
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM