You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从CSHTML提取JS时遇TS5055错误及文件命名404问题求助

解决方案

解决TS5055错误(List.cshtml.js文件)

该错误是因为Visual Studio的TypeScript服务误将.cshtml.js文件识别为TypeScript编译源,尝试生成同名输出文件导致冲突,可通过以下方式解决:

  • 修改TypeScript配置(tsconfig.json):
    在tsconfig.json的exclude数组中添加**/*.cshtml.js,让TypeScript服务忽略这类文件:

    {
      "compilerOptions": {
        // 保留原有配置
      },
      "exclude": [
        "node_modules",
        "**/*.cshtml.js"
      ]
    }
    
  • 调整文件BuildAction:
    右键List.cshtml.js文件 → 属性 → 将Build Action设置为Content(而非默认的TypeScriptCompile),避免VS将其当作TypeScript编译文件处理。

解决List.scripts.js的404错误

404问题核心是ASP.NET Core默认不暴露Views目录下的静态文件,且文件可能未被正确配置为可访问资源:

  • 配置静态文件中间件访问Views目录:
    在Program.cs中添加额外配置,允许访问Views下的静态资源:

    var builder = WebApplication.CreateBuilder(args);
    
    // 保留现有服务配置代码
    
    var app = builder.Build();
    
    // 先启用wwwroot目录的静态文件支持
    app.UseStaticFiles();
    
    // 添加Views目录的静态文件支持
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(builder.Environment.ContentRootPath, "Views")),
        RequestPath = "/Views"
    });
    
    // 保留其他中间件配置(路由、授权等)
    app.Run();
    
  • 确保文件被正确发布:
    右键List.scripts.js文件 → 属性 → 确认Build Action为Content,Copy to Output Directory设置为Copy if newer,保证发布时文件被复制到输出目录。

  • 修正CSHTML中的引用路径:
    在CSHTML中使用根相对路径引用脚本,示例:

    <script src="@Url.Content("~/Views/[你的控制器名]/List.scripts.js")"></script>
    

    或使用Tag Helper添加版本戳:

    <script src="~/Views/[你的控制器名]/List.scripts.js" asp-append-version="true"></script>
    

内容的提问来源于stack exchange,提问作者Steve Py

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 11:52:48