如何在ASP .NET Core Web API中映射后备,以便Blazor WASM应用程序仅拦截不属于该API的请求

问题描述 投票:0回答:1

根据Microsoft的默认解决方案模板,我有一个Blazor WebAssembly解决方案,其中包含客户端项目,服务器项目和共享项目。我正在使用Google Chrome在Visual Studio 2019预览版中进行编辑和调试。

开箱即用,该解决方案只有一个启动项目,即服务器应用程序。该服务器应用程序具有对客户端应用程序的项目引用。您可以通过在服务器项目属性中选中“启用SSL”来将其设置为使用HTTPS,而我已经做到了。

当您单击调试时,它可以正常工作。

现在,我想对其进行更改,以使Blazor WASM应用程序仅响应https://localhost:44331的请求,而不响应https://localhost:44331/api的请求。这些请求应改为由服务器应用程序的API控制器端点处理。因此,如果有人访问https://localhost:44331/api/something,并且不存在这样的API终结点,则他们应该从API收到404错误代码,并且不要路由到通常的Blazor页面,说“对不起,此地址没有任何内容。”

我想使用URL的这个额外的“ / api”部分,以使对API的请求与对页面的请求分开。我认为这将更接近于正常的生产环境。我希望很清楚我要做什么。

这里是带有路由属性的示例Controller声明:

namespace BlazorApp2.Server.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        // Etc.

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            //etc.
        }
///etc.
    }
}

这是我在Startup.cs中尝试过的方法,它不起作用。有人可以建议一些吗?

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Etc.
    app.UseStatusCodePages();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        // The line commented out below is the out-of-the-box behaviour for a Blazor WASM app with ASP NET Core API. This is the line I want to replace.
        // endpoints.MapFallbackToFile("index.html");

        // The line below is my (failed) attempt to get the behaviour I want.
        endpoints.MapFallback(HandleFallback);
    });
}

private async Task HandleFallback(HttpContext context)
{
    var apiPathSegment = new PathString("/api"); // Find out from the request URL if this is a request to the API or just a web page on the Blazor WASM app.

    bool isApiRequest = context.Request.Path.StartsWithSegments(apiPathSegment);

    if (!isApiRequest)
    {
        context.Response.Redirect("index.html"); // This is a request for a web page so just do the normal out-of-the-box behaviour.
    }
    else
    {
        context.Response.StatusCode = StatusCodes.Status404NotFound; // This request had nothing to do with the Blazor app. This is just an API call that went wrong.
    }
}

有人知道如何按照我的意愿来工作吗?

asp.net-core blazor iis-express blazor-client-side asp.net-core-staticfile
1个回答
0
投票

[如果您从Blazor WASM托管解决方案开始,您将获得一个示例,只需启动

dotnet new blazorwasm --hosted

它创建了一个包含3个项目的解决方案:

|-客户|-服务器|-共享

在Server Startup类中,中间件管道的设置如下:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseWebAssemblyDebugging();
    }
    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.UseBlazorFrameworkFiles();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapFallbackToFile("index.html");
    });
}

控制器使用Route属性定义其路线。如果要将控制器托管在/ api / {controller},请使用Route属性值Route

api/[controller]
© www.soinside.com 2019 - 2024. All rights reserved.