繁体   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