为 blazor.webassembly.js 获取 404 NotFound(未找到_framework 文件) [英] Getting 404 NotFound for blazor.webassembly.js (_framework files not found)

查看:40
本文介绍了为 blazor.webassembly.js 获取 404 NotFound(未找到_framework 文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们沿着这条线搜索了其他问题,发现它们似乎适用于部署站点之后.尝试调试时,这会在 VS 2019 中发生.到目前为止,我们已检查的内容包括:

applicationhost.config 看起来是正确的,虚拟目录路径确实是client"所在的位置.应用程序已植根而wwwroot文件夹是物理路径的子文件夹.

 <站点名称=Eclypses.MTE.Protoype.Server"id=8"><应用程序路径=/";applicationPool=Eclypses.MTE.Protoype.Server AppPool"><virtualDirectory path="/";physicalPath=D:git_srcmteutils_dotnetcoreeclypses-commander-prototypeEclypses.MTE.PrototypeClient";/></应用程序><绑定><绑定协议=http";bindingInformation="*:50413:localhost";/></绑定></网站>

我们在服务器启动时记录一条信息消息,它确实出现在我们的日志文件中.

客户端的launchsettings.json文件是:

<代码>{iis设置":{windowsAuthentication":假,匿名身份验证":真,iisExpress":{applicationUrl":http://localhost:50413",ssl端口":0}},个人资料":{IIS Express":{命令名称":IISExpress",launchBrowser":真,环境变量":{ASPNETCORE_ENVIRONMENT":开发"},use64Bit":真,inspectUri":{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}";},Eclypses.MTE.Prototype":{命令名称":项目",launchBrowser":真,环境变量":{ASPNETCORE_ENVIRONMENT":开发"},applicationUrl":https://localhost:5001;http://localhost:5000",inspectUri":{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}";}}}

服务器的launchsettings.json文件是:

<代码>{iis设置":{windowsAuthentication":假,匿名身份验证":真,iisExpress":{applicationUrl":http://localhost:50413",ssl端口":0}},个人资料":{IIS Express":{命令名称":IISExpress",launchBrowser":真,环境变量":{ASPNETCORE_ENVIRONMENT":开发"},use64Bit":真,inspectUri":{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}";},Eclypses.MTE.Prototype.Server":{命令名称":项目",launchBrowser":真,环境变量":{ASPNETCORE_ENVIRONMENT":开发"},applicationUrl":https://localhost:5001;http://localhost:5000",inspectUri":{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}";}}}

我们将两者的 sslPort 更改为0";通过取消选中启用 SSL";属性页上的复选框.(我们尝试使用 SSL 并得到同样的错误).

我们还注释掉了服务器启动类中的一行:

//app.UseHttpsRedirection();

所以我们应该在 VS 2019(目前版本为 16.8.5)中运行一个纯 http 环境.

最后,我们确保客户端和服务器都使用最新版本的相应 NuGet 包:客户:

服务器:

 

csproj 文件使用以下 Sdk 和框架(在客户端上,我们尝试了两种 Sdk,结果相同).客户:

<!--Project Sdk="Microsoft.NET.Sdk.Web"--><属性组><TargetFramework>net5.0</TargetFramework></PropertyGroup></项目>

服务器:

<属性组><TargetFramework>net5.0</TargetFramework></PropertyGroup></项目>

服务器的启动类按以下顺序配置:

 if (env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseWebAssemblyDebugging();}别的{app.UseExceptionHandler("/Error");//默认的 HSTS 值为 30 天.您可能希望针对生产场景更改此设置,请参阅 https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseBlazorFrameworkFiles();app.UseStaticFiles();//app.UseHttpsRedirection();app.UseRouting();app.UseEndpoints(endpoints =>{端点.MapRazorPages();端点.MapControllers();endpoints.MapFallbackToFile("index.html");});

客户端的 Program.cs 类按以下顺序配置:

var builder = WebAssemblyHostBuilder.CreateDefault(args);builder.RootComponents.Add<App>(app");建设者.服务.AddBlazoredModal().AddOptions().AddAuthorizationCore().AddSingleton<ISessionStorageService, SessionStorageService>()//封装了 javascript 会话存储方法.AddScoped<IAuthService, AuthService>()//封装 API 调用服务器以验证登录.AddScoped()//创建用户主体.AddSingleton();//包装一些本地状态";价值观builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });等待 builder.Build().RunAsync();

在研究了多个帖子后,以上是我们认为需要检查的内容.根据我们的理解,它们看起来是正确的,但我们仍然会从 _framework 和 _content 文件夹中加载任何内容,而且更重要的是 blazor.webassembly.js" 文件不存在?!?!?

当我们开始调试会话时,VS 中会出现一个图标,用于脚本文件"里面有一个图标本地主机:50413/js/sessionStorage.js"因此,运行时似乎找到了 wwwroot,但没有_framework".或_content"文件夹,因此它无法加载我的 blazor WASM 文件.当我们终止调试会话时,该图标就会消失.

此外,如果我们在 Chrome 中查看网络选项卡,我们会看到使用200"下载的 wwwroot 文件的内容,但 blazor.webassembly.js(应该在 _framework 中)得到 404.

感谢这里的任何指导,基本问题是为什么我们会收到404"?_framework 和 _content 文件?

解决方案

确保您的服务器包含对客户端的项目引用.还要确保您客户的 csproj 文件使用

<属性组><TargetFramework>net5.0</TargetFramework></PropertyGroup>

We have searched other issues along this line, and found that they seem to apply to after a site is deployed. This happens within VS 2019 when trying to debug. The things we have checked so far include:

The applicationhost.config which appears to be correct, the virtual directory path is indeed where the "client" app is rooted and the wwwroot folder is a subfolder of the physical path.

 <site name="Eclypses.MTE.Protoype.Server" id="8">
    <application path="/" applicationPool="Eclypses.MTE.Protoype.Server AppPool">
      <virtualDirectory path="/" physicalPath="D:git_srcmteutils_dotnetcoreeclypses-commander-prototypeEclypses.MTE.PrototypeClient" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:50413:localhost" />
    </bindings>
  </site>

We log an info message at the server startup and it does indeed appear in our log files.

The launchsettings.json file for the client is:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50413",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "use64Bit": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}"
    },
    "Eclypses.MTE.Prototype": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}"
    }
  }
}

and the launchsettings.json file for the server is:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50413",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "use64Bit": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}"
    },
    "Eclypses.MTE.Prototype.Server": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}"
    }
  }
}

We changed the sslPort on both to "0" by un-checking the "Enable SSL" checkbox on the property pages. (We tried with SSL and got the same error).

We also commented out the line in the server startup class:

// app.UseHttpsRedirection();

so we should be running a pure http environment in VS 2019 (which is currently at version 16.8.5).

Finally, we ensured that both the client and the server are using the latest versions of the appropriate NuGet packages: Client:

<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.3" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />

Server:

  <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.3" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
    <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
    <PackageReference Include="System.Runtime.Caching" Version="5.0.0" />

The csproj files use the following Sdk and Framework {On the client we tried both Sdks with the same result). Client:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<!--Project Sdk="Microsoft.NET.Sdk.Web"-->
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>

Server:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>

The Startup Class for the server configures in the following order:

    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.UseBlazorFrameworkFiles();
    app.UseStaticFiles();
    // app.UseHttpsRedirection();
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        endpoints.MapFallbackToFile("index.html");
    });

The Program.cs class for the client configures in the following order:

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");

builder.Services
    .AddBlazoredModal()
    .AddOptions()
    .AddAuthorizationCore()
    .AddSingleton<ISessionStorageService, SessionStorageService>() // Wrapper around javascript session storage methods
    .AddScoped<IAuthService, AuthService>() // Wrapper around API calls to server to authenticate a login
    .AddScoped<AuthenticationStateProvider, MyWebAuthenticationStateProvider>() // Creates a user principal
    .AddSingleton<StateContainer>();  // Wrapper around some local "state" values

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();

After researching multiple posts, the above things are what we felt we needed to check. They look correct from what we understand, yet we still get a 404 loading anything from the _framework and _content folders and more importantly the "blazor.webassembly.js" file just is not there?!?!?

When we start a debug session, an icon appears in VS for "Script Documents" and within it is an icon for "localhost:50413/js/sessionStorage.js" so, it appears that the wwwroot is found by the runtime, but there is no "_framework" or "_content" folders, so it cannot load my blazor WASM file. This icon goes away when we kill the debug session.

Also, if we view the network tab in Chrome, we see the contents of the wwwroot file downloaded with a "200", but the blazor.webassembly.js (which should be in _framework) gets a 404.

Any guidance here is appreciated, the basic question is "Why are we getting the "404" for _framework and _content files?

解决方案

Make sure your Server includes a project reference to your Client. Also make sure your client's csproj file uses

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

这篇关于为 blazor.webassembly.js 获取 404 NotFound(未找到_framework 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆