【问题标题】:ASP.NET Core 3.1 - routing IssueASP.NET Core 3.1 - 路由问题
【发布时间】:2020-11-15 20:22:41
【问题描述】:

自从升级到 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();
        });
    }
}

【问题讨论】:

    标签: asp.net-core cors


    【解决方案1】:

    中间件顺序很重要。将您的UseSpa() 放在UseEndpoints() 之后

    // 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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多