ASP.Net Core:使用共享项目在许多项目之间共享静态资产(css/js) [英] ASP.Net Core: Use Shared Project to share static assets (css / js) between many projects

查看:345
本文介绍了ASP.Net Core:使用共享项目在许多项目之间共享静态资产(css/js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这里汲取灵感:

Taking inspiration from here: http://rion.io/2017/03/22/sharing-is-caring-using-shared-projects-in-asp-net/, I'm trying to use Shared Projects to share static assets (e.g. css / js) between multiple ASP.Net Core projects.

我创建了一个共享项目,并将此文件添加到此文件夹层次结构中:

I created a shared project and added this file in this folder hierarchy:

然后,我从ASP.Net Core Web项目中添加了对该共享项目的引用.但是,我在运行项目时无法访问此文件.我尝试了这些网址(还有许多其他网址):

I then added a reference to this shared project from my ASP.Net Core web project. However, I'm not able to access this file when I run the project. I tried these URLs (among many others):

http://localhost:50000/assets.style.css
http://localhost:50000/wwwroot/assets/style.css

它们都导致404.看来我要么没有找到使这些文件在我的Web项目中可用的方法,要么我引用不正确.有人知道如何使这项工作吗?

They all result in 404. It seems like I either haven't found a way to make these files available in my web project or I'm referencing them incorrectly. Anyone know how to make this work?

推荐答案

这是我解决此问题的方法.我没有使用共享项目,因为我无法使用它.取而代之的是,我使用了PhysicalFileProvider,它的作用就像魅力.

Here's how I solved this problem. I didn't use a Shared Project because I couldn't get that to work. Instead, I used PhysicalFileProvider and it works like charm.

为此,我在src中创建了一个新文件夹.我将其命名为assets,因此我的解决方案资源管理器看起来像这样:

To do this, I created a new folder in src. I called it assets so my Solution Explorer looks something like this:

src/assets
src/Foo.Web1
src/Foo.Web2
test/Foo.Tests

我将所有共享的静态资产(例如css,JavaScript,图像)放在/src/assets中.现在,我使用PhysicalFileProvider/src/assets映射到Foo.Web1Foo.Web2项目中的wwwroot/assets文件夹.在Startup.csConfigure方法中,添加以下内容:

I put all of the shared static assets (e.g. css, JavaScript, images) in /src/assets. Now I map /src/assets to the wwwroot/assets folders in Foo.Web1 and Foo.Web2 projects using PhysicalFileProvider. In Startup.cs's Configure method, I add this:

if (Env.IsDevelopment())
{
    // app is IApplicationBuilder and Env is IHostingEnvironment
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(Regex.Replace(Env.ContentRootPath, $"{Env.ApplicationName}$", "assets"))),
        RequestPath = new PathString("/assets"),
        OnPrepareResponse = ctx =>
        {
            // Optionally manipulate the response as you'd like. for example, I'm setting a no cache directive here for dev.
            ctx.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
        }
    });
}

现在,来自/src/assets文件夹的所有请求都将由Foo.Web1Foo.Web2可以共享的/src/assets文件夹提供. /src/assets中的所有更改将实时反映.这项工作在开发人员中非常出色,但是在生产中,我不这样做.相反,我在部署脚本中将文件从/src/assets复制到Foo.Web1/wwwroot/assetsFoo.Web2/wwwroot/assets,因此不需要重新映射.

Now any request that comes to /assets will be served from the /src/assets folder that can be shared by Foo.Web1 and Foo.Web2. All changes in /src/assets will be reflected in real-time. This work great in dev, but when it comes to production, I don't do this. Instead, I copy the files from /src/assets to Foo.Web1/wwwroot/assets and Foo.Web2/wwwroot/assets in my deployment script so this remapping doesn't need to happen.

这篇关于ASP.Net Core:使用共享项目在许多项目之间共享静态资产(css/js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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