在 Blazor 服务器 Web 应用程序中托管 Web API 时出错

时间:2020-12-19 00:30:33

标签: asp.net-core asp.net-core-webapi blazor-server-side asp.net-web-api-routing

我们有一个现有的 .NET 5.0 Blazor Web 应用程序。我在同一个项目中添加了一个 ASP.NET Core Web API,因为我想为外部使用者提供一个 REST 接口。当我请求 http://localhost:5000/stripe/customerwebhook 时,我收到 404 not found 错误。我可能会遗漏什么?

我的 CustomerWebhookController API 类如下所示:

[Route("stripe/[controller]")]
[ApiController]
public class CustomerWebhookController : ControllerBase
{        
    [HttpPost]
    public async Task<IActionResult> Index()
    {            
            return Ok();
    }
}

Startup.cs:

using Microsoft.AspNetCore.Mvc;
public partial class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            OnConfiguringServices(services);
            services.AddHttpContextAccessor();
            services.AddScoped<HttpClient>(serviceProvider =>
            {
              var uriHelper = serviceProvider.GetRequiredService<NavigationManager>();
              return new HttpClient
              {
                BaseAddress = new Uri(uriHelper.BaseUri)
              };
            });
            services.AddHttpClient();
            services.AddAuthentication();
            services.AddAuthorization();
            services.AddControllersWithViews();
            services.AddMvc(options => options.EnableEndpointRouting = false)
                .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            services.AddRazorPages();
            services.AddServerSideBlazor().AddHubOptions(o =>
            {
                o.MaximumReceiveMessageSize = 10 * 1024 * 1024;
            });
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationIdentityDbContext identityDbContext)
        {
            OnConfiguring(app, env);
            if (env.IsDevelopment())
            {
                Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.Use((ctx, next) =>
                {
                    return next();
                });
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseMvcWithDefaultRoute();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                  name: "default",
                  pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }

0 个答案:

没有答案