【问题标题】:Angular Universal + Asp Net Core: how to serve static file in virtual directoryAngular Universal + Asp Net Core:如何在虚拟目录中提供静态文件
【发布时间】:2020-02-24 05:56:31
【问题描述】:

我有一个 Angular Universal + Asp Net Core 应用程序。

我需要从“虚拟目录”提供静态文件。

在 ASP NET 方面我有:

app.UseFileServer(new FileServerOptions
        {
            FileProvider = new PhysicalFileProvider(Configuration["CDN"]),
            RequestPath = new PathString("/cdn"),
            EnableDirectoryBrowsing = false
        });

在 Angular Universal 方面我尝试过:

app.use(express.static('CDN'));

app.use('/CDN', express.static('C:\path\to\CDN'));

但是这些都不起作用,我收到了这个错误:

main-es2015.js:1 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'CDN/photot.jpg'

【问题讨论】:

    标签: angular asp.net-core angular-universal


    【解决方案1】:

    我解决了更改 server.ts;

    app.get('*', (req, res) => {
    if(req.url.indexOf('CDN/')>-1){
        res.sendFile(req.url,{ root: 'C:\\path\\to\\cdn' });
    } else {
        res.render('index', { req });
    }});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多