Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Blazor WebAssembly应用程序-来自浏览器的API调用-抱歉,有';这个地址什么也没有_C#_Asp.net Core_Asp.net Web Api_Blazor_Blazor Webassembly - Fatal编程技术网

C# Blazor WebAssembly应用程序-来自浏览器的API调用-抱歉,有';这个地址什么也没有

C# Blazor WebAssembly应用程序-来自浏览器的API调用-抱歉,有';这个地址什么也没有,c#,asp.net-core,asp.net-web-api,blazor,blazor-webassembly,C#,Asp.net Core,Asp.net Web Api,Blazor,Blazor Webassembly,更新: 在Github ASP.NET核心项目上创建了一个问题: 原件: 使用Microsoft Visual Studio 2019版本16.9.4创建了一个新的Blazor WebAssembly应用程序,目标框架为.NET 5.0,托管ASP.NET核心 问题是,如果我在网站发布到Azure Web应用程序时从浏览器调用API,我会得到如下错误响应: && !event.request.url.includes('/api/') 对不起,这个地址什么也没有 如果我用“

更新:

在Github ASP.NET核心项目上创建了一个问题:

原件:

使用Microsoft Visual Studio 2019版本16.9.4创建了一个新的Blazor WebAssembly应用程序,目标框架为.NET 5.0,托管ASP.NET核心

问题是,如果我在网站发布到Azure Web应用程序时从浏览器调用API,我会得到如下错误响应:

&& !event.request.url.includes('/api/')
对不起,这个地址什么也没有

如果我用“空缓存和硬重新加载”发出请求,一切都会按预期工作

<script>navigator.serviceWorker.register('service-worker.js');</script>

请求始终在
localhost
上工作

它不限于通过API加载的图像,对于
JSON响应
,结果相同

如何告诉
Blazor
不要对包含
/api/
的URL使用/忽略路由

我读过关于ASP.NET Core Blazor路由的

错误消息来自
App.razor
文件:

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
        <Found Context="routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>
<MatPortalHost></MatPortalHost>
图像控制器:

[Route("api/[controller]")]
[ApiController]
[Authorize]
public class ImagesController : ControllerBase
{
    private readonly ApplicationDbContext _context;

    public ImagesController(ApplicationDbContext context)
    {
        _context = context;
    }

    [HttpGet("{id}/file")]
    [AllowAnonymous]
    public async Task<ActionResult> GetImageDataFile(int id)
    {
        var image = await _context.Images.FindAsync(id);

        if (image == null)
        {
            return NotFound();
        }

        return File(image.Data, image.ContentType);
    }
Blazor.Server
Startup.cs
,完全未修改:

namespace Blazor.Server
{
    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.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

            services.AddAuthentication()
                .AddIdentityServerJwt();

            services.AddControllersWithViews();
            services.AddRazorPages();
        }

        // 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();
                app.UseMigrationsEndPoint();
                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.UseIdentityServer();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapFallbackToFile("index.html");
            });
        }
    }
}
namespace Blazor.Server
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{

services.AddDbContext

调试起来确实很痛苦,但我最终发现了问题

TLDR

Blazor\Client\wwwroot\index.html
中删除以下行:

<script>navigator.serviceWorker.register('service-worker.js');</script>
然后,我尝试使用
Progressive Web Application
集创建一个新项目,并使用以下设置将其部署到本地IIS:

加载站点后,我无法访问
https://localhost/_framework/blazor.boot.json
没有看到
对不起,这个地址什么都没有。

如果您想继续使用
渐进式Web应用程序
服务工作者
,您可以编辑
服务工作者.published.js
并更新
shouldServeIndexHtml
以强制服务器呈现URL,如下所示:

&& !event.request.url.includes('/api/')

你检查过你的应用程序设置和restApi的url吗?可能你的rest api的url仍然是localhost。你必须将其更改为服务器发布的地址,但我不确定这是否有帮助。我根本没有修改
Startup.cs
,只要
Blazor
没有被禁用,api控制器就可以工作aded。Blazor客户端也不应该使用HTTP客户端配置,请求应该直接发送到服务器并返回响应,而Blazor不会干预。我正在尝试帮助。这很奇怪。这不会发生在本地主机上。Hrmm可以可靠地命中它吗?@BrianParker非常感谢,我无意粗鲁。正确吗这种情况不会发生在本地主机上。Postman工作正常。我在尝试加载
/。浏览器中的著名/openid配置
/\u framework/blazor.boot.json
时也会遇到同样的错误。@BrianParker如果没有
endpoints.MapControllers()
使用该请求时,邮递员或使用“空缓存和硬重新加载”将无法处理该请求。我同意主机配置,但我不知道它可能是什么。最好我想告诉Blazor总是让对
/api/*
的请求通过,而Blazor路由不介入。是的,我一读就删除了它。呵呵。尝试过其他浏览器吗?不-url可以在评论中看到。查看我的答案,了解我修复了什么t、 原来是
渐进式Web应用程序
和包含的
服务人员
&& !event.request.url.includes('/api/')