您如何将Nuget程序包将文件复制到DotNet Core项目中? [英] How Do You Have A Nuget Package Copy Files Into A DotNet Core Project?

查看:316
本文介绍了您如何将Nuget程序包将文件复制到DotNet Core项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Nuget程序包,该程序包会将一些文件复制到应用程序的wwwroot文件夹中,但是尽管阅读了文档并在线查看了其他问题,但我似乎无法使其工作.这是我到目前为止所拥有的.

I'm trying to create a Nuget package that will copy some files into the wwwroot folder of the application, but I can't seem to get it to work despite reading the documentation and looking at other questions online. Here is what I have so far.

<?xml version="1.0"?>
<package >
  <metadata>
    <id>...</id>
    <version>...</version>
    <title>...</title>
    <authors>...</authors>
    <requireLicenseAcceptance>...</requireLicenseAcceptance>
    <description>...</description>
    <contentFiles>
      <files include="**/*.*" copyToOutput="true" buildAction="EmbeddedResource" />
    </contentFiles>
  </metadata>
  <files>
    <file src="ProjectName\bin\Release\**.dll" target="lib" />
    <file src="ProjectName\wwwroot\**.*" target="content\wwwroot" />
    <file src="ProjectName\wwwroot\**.*" target="contentFile\any\any\wwwroot" />
  </files>
</package>

它按预期将文件放入Nuget包的content和contentFile文件夹中,但是如果引用,则不会将文件复制到项目中.

It puts the files into the content and contentFile folders of the Nuget package as expected, but when referenced if does not copy the files into the project.

推荐答案

因此,在检查了建议的链接和互联网的其余部分之后,这是我想出的解决方案.

So, after checking the suggested link and the rest of the internet, here is the solution I came up with.

public static class ApplicationBuilderExtensions
{
    public static IApplicationBuilder UseMyApp(this IApplicationBuilder app) => UseMyApp(app, null);

    public static IApplicationBuilder UseMyApp(this IApplicationBuilder app, Action<Options> setupOptions)
    {
        var options = new Options();
        setupOptions?.Invoke(options);

        app.UseStaticFiles(new StaticFileOptions
        {
            RequestPath = $"/{options.RoutePrefix}",
            FileProvider = new EmbeddedFileProvider(Assembly.Load(new AssemblyName("MyAssembly")), "MyAssembly.EmbeddedResourcesFolder")
        });

        return app;
    }

    public class Options
    {
        internal Options() { }

        public string RoutePrefix { get; set; } = "defaultRoutePrefix";
    }
}

然后,您可以在html中通过执行以下操作来引用静态文件.

Then, in your html you can reference the static files by doing something like this.

<link rel="stylesheet" href="~/routePrefix/fileName.extension" />

从技术上讲,这并不是我所说问题的答案,但是现在很明显,我要求做的不是最佳实践-这是一种更好的解决方法.

It is not technically an answer to my question as stated, but it is clear now that what I was asking to do is not best practice - this is a much better way to do it.

这篇关于您如何将Nuget程序包将文件复制到DotNet Core项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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