【问题标题】:How to embed resource files at build in Visual Studio 2017?如何在 Visual Studio 2017 中嵌入资源文件?
【发布时间】:2017-07-29 12:16:22
【问题描述】:

我有一个 nuget 包,它有自己的 mvc 控制面板,带有控制器、视图和路由。

这个 nuget 包被导入到其他 .net core web 应用程序中。

在 Visual Studio 2015 中,使用 .net 内核,我使用以下代码将视图编译为资源,使它们能够被 razor 引擎找到并正确呈现。

在 project.json (nuget):

 "buildOptions": {
    "embed": "**/Views/**/*.cshtml"
  }

在 Startup.cs(网络应用程序):

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<RazorViewEngineOptions>(options =>
    {
        options.FileProviders.Add(new CompositeFileProvider(
            new EmbeddedFileProvider(
                typeof(HomeController).GetTypeInfo().Assembly,
                "Some.Namespace"))
                    );
    });

    return new IuguPortalBuilder(services);
}

在 Visual Studio 2017 中,project.json 文件不再存在,我无法找到将视图嵌入到 nuget 包中的新方法。

如何嵌入我的视图?

【问题讨论】:

    标签: asp.net-mvc visual-studio asp.net-core visual-studio-2017


    【解决方案1】:

    Solution Explorer 中右键单击所需文件,单击Properties 并在打开的窗口中将Build action 设置为Embedded resource - 一切都像过去一样:)

    这将为您的*.csproj 文件创建以下行:

    <ItemGroup>
      <EmbeddedResource Include="Views\Home\Index.cshtml" />
    </ItemGroup>
    

    现在 MSBuild 会将此文件作为嵌入资源添加到您的程序集中。

    【讨论】:

    • 嗨@Dmitry!我可以使用“Views/*/*.cshtml”之类的通配符一次性导入我的所有视图吗?
    • @Carlos,我不知道,试着分享一下:)
    • 刚刚在 VS2017 上试过:它对我有用,格式类似于 Include="**\Translations\Resource.*.resx"
    • 来自解决方案资源管理器。将构建操作设置为嵌入资源不起作用。所以我手动编辑了csproj,就像你在这里展示的那样。使用此方法文件可以在磁盘上的任何位置。
    猜你喜欢
    • 1970-01-01
    • 2011-01-23
    • 2021-10-16
    • 2021-07-28
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多