【问题标题】:Is there a way to package an ASP.NET Core app area as a NuGet package?有没有办法将 ASP.NET Core 应用程序区域打包为 NuGet 包?
【发布时间】:2016-09-09 21:08:41
【问题描述】:

我正在开发一个 ASP.NET Core 应用程序,我希望将它作为 NuGet 包发布,您可以将它添加到任何 Core Web 项目中。该应用程序实际上完全局限于项目中的一个区域(即/Areas/MyArea),包括控制器、视图、服务类、模型、视图等,除了少数几个。真的,这些是我很想神奇地添加到现有网络应用程序的部分:

  • 该地区及其中的一切
  • 它的 CSS 和 JS 在 wwwroot/lib/myapp 中
  • Startup 类中的条目
  • MyApp.json 在根目录中

我知道 NuGet 会恢复包依赖项,但我不确定它会如何考虑客户端包。

有什么建议吗? NuGet 是不是为此提供了错误的工具?

【问题讨论】:

    标签: asp.net-core nuget asp.net-core-mvc


    【解决方案1】:

    目前 afaik 无法将文件从 nuget 包传递到 Web 应用程序。我认为关于在未来实现这一点的一些讨论和工作正在进行中。

    我在项目中处理该问题的方式是在 project.json 中嵌入视图和所需的静态 js 和 css 资源:

    “构建选项”:{ “嵌入”:[“视图/”、“js/”、“css/**”] },

    我创建了一个controller to serve my static resources

    public class cscsrController : Controller
    {  
        private ContentResult GetContentResult(string resourceName, string contentType)
        {
            var assembly = typeof(cscsrController).GetTypeInfo().Assembly;
            var resourceStream = assembly.GetManifestResourceStream(resourceName);
            string payload;
            using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
            {
                payload = reader.ReadToEnd();
            }
    
            return new ContentResult
            {
                ContentType = contentType,
                Content = payload,
                StatusCode = 200
            };
        }
    
        [HttpGet]
        [AllowAnonymous]
        public ContentResult bootstrapdatetimepickercss()
        {
            return GetContentResult(
                "cloudscribe.Core.Web.css.bootstrap-datetimepicker.min.css",
                "text/css");
        }
    
        [HttpGet]
        [AllowAnonymous]
        public ContentResult momentwithlocalesjs()
        {
            return GetContentResult(
                "cloudscribe.Core.Web.js.moment-with-locales.min.js",
                "text/javascript");
        }
    
    }
    

    然后我在需要加载 js 和/或 css 的视图中链接到控制器操作。

    为了让嵌入式视图正常工作,我创建了 RazorViewEngineOptions 的扩展方法:

    public static RazorViewEngineOptions AddEmbeddedViewsForCloudscribeCore(this RazorViewEngineOptions options)
    {
        options.FileProviders.Add(new EmbeddedFileProvider(
                typeof(SiteManager).GetTypeInfo().Assembly,
                "cloudscribe.Core.Web"
            ));
    
        return options;
    }
    

    这必须从 Web 应用 Startup 中的 ConfigureServices 调用,如下所示:

    services.AddMvc()
        .AddRazorOptions(options =>
        {
            options.AddEmbeddedViewsForCloudscribeCore();
    
        })
        ;
    

    这种技术应该适用于区域。请注意,一件很酷的事情是用户可以下载视图并在本地安装它们,这将覆盖嵌入式视图的使用,从而可以轻松自定义部分或全部视图。通过覆盖视图,如果需要,还可以在本地手动安装 js 和 css,并在需要自定义时更改视图以链接到这些本地文件。最终结果是我的 nuget 拥有所需的一切,因此只需进行一些启动配置即可让一切正常运行。

    【讨论】:

    • 我不确定你不能添加文件是真的。一些 DI 容器库可以做到这一点(例如,StructureMap MVC),我想使用 PowerShell 脚本。
    • 实际上您可以将文件添加到包中,但您无法将它们交付到当前用于 .NET Core nugets afaik github.com/NuGet/Home/issues/2262 的应用程序中
    • 我知道 powershell 现在是开源和跨平台的,但不确定你是否真的可以在跨平台的方式中使用它,至少目前可以使用 nuget
    • 这个问题是关于你可以在 nuget 中获取视图但你不能将它们导出 github.com/aspnet/Home/issues/1490
    • 谢谢,乔,这些链接真的很好。第一个正是我已经陷入的周期性文档混乱,但第二个实际上在嵌入视图方面很有希望。也许静态内容从那里很容易。
    【解决方案2】:

    多年后,通过使用 Razor 类库,这在 ASP.NET Core > v2.x 中成为可能。

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多