簡體   English   中英

ASP.NET Core 3.1 - 路由問題

[英]ASP.NET Core 3.1 - routing Issue

自從升級到 3.1 后,我似乎失去了路由。 我的端點現在只是返回一個 404,我已經想不出問題可能是什么了。 誰能看到我在下面的設置代碼中是否在做一些愚蠢的事情? 我的設置代碼中真正改變的是我停止使用

app.UseMvc

現在改用端點

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

下面的代碼

[ApiController]
[Authorize]
[Route("api/guild")]
public class GuildController : Controller
{
    [HttpPost("create")]        
    public async Task<IActionResult> Create([FromBody]CreateGuildRequest request)
    {
        return Ok();
    }
}

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {            
        var domain = Configuration["Auth0:Domain"];

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options =>
        {
            options.Authority = domain;
            options.Audience = Configuration["Auth0:Audience"];
        });
        
        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .SetIsOriginAllowed(origin => true) 
                    .AllowCredentials()
                    .Build());
        });
        
        services.AddControllers();
        
        services.AddSpaStaticFiles(configuration => { configuration.RootPath = "clientapp/dist"; });
        
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();
        app.UseRouting();

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "clientApp";

            if (env.IsDevelopment())
            {
                spa.UseVueCli(npmScript: "serve", port: 8080);
            }
        });
        
        app.UseCors("CorsPolicy");
        app.UseAuthentication();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

中間件順序很重要。 將 UseSpa() 放在UseEndpoints() UseSpa()之后

// this should be the last two middlewares, in this order
           
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "clientApp";

    if (env.IsDevelopment())
    {
        spa.UseVueCli(npmScript: "serve", port: 8080);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM