如何在 MVC6 中使用标准类库? [英] How do I use a standard class library in MVC6?

查看:34
本文介绍了如何在 MVC6 中使用标准类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种相当方便的方法可以在 MVC6 中包含标准类库?我认为标准的添加引用对话框可以创建一个包装器,但我没有任何运气.将它发布为 nuget 包对于正在进行的开发调试来说似乎非常不方便.

Is there a reasonably convenient way to include a standard class library in MVC6? I thought the standard add reference dialog could create a wrapper but I'm not having any luck. Publishing it as a nuget package seems pretty inconvenient for ongoing development debugging.

如果没有包装器,是否有将标准类库转换为新类型的内置方法?

If there isn't a wrapper is there a built in way to convert the standard class library to the new type?

推荐答案

到目前为止,我已经看到了几种不同的方法(使用 dnu 实用程序或使用 VS 添加引用).根据您是要在解决方案中引用现有程序集还是引用非 asp 5 项目,它也略有不同.

I have seen a couple of different approaches so far (using the dnu utility or adding references using VS). It is also slightly different depending if you want to reference an existing assembly or to reference a non asp 5 project within your solution.

当您想要添加对不属于您的解决方案的现有程序集的引用时:

  • 使用dnu wrap 命令添加引用.您可以打开开发人员命令提示符并导航到您的解决方案文件夹,或者只需在 Visual Studio 中打开包管理器控制台即可.

  • Use the dnu wrap command to add the reference. You can open a developer command prompt and navigate to your solution folder or even easier just open the package manager console inside visual studio.

现在,如果您想添加对 fullPathToYourDllFoo.dll 的引用,您将运行以下命令(包装程序集时需要框架参数,并定义为 Target要包装的程序集框架):

Now if you want to add a reference to fullPathToYourDllFoo.dll you will run the following command (where the framework argument is required when wrapping an assembly, and is defined as Target framework of assembly to be wrapped):

dnu wrap "C:fullPathToYourDllFoo.dll" -f 4.5.1

这将生成/更新 asp 5 解决方案文件夹中的 wrap 文件夹,包括一个文件 mySolutionwrapFooproject.json,如下所示:>

This will generate/update the wrap folder inside your asp 5 solution folder, including a file mySolutionwrapFooproject.json that looks like:

{
  "version": "1.0.0-*",
  "frameworks": {
    "net451": {
      "bin": {
        "assembly": "../../../FooSln/Foo/bin/Debug/Foo.dll",
        "pdb": "../../../FooSln/Foo/bin/Debug/Foo.pdb"
      }
    }
  }
}

最后,更新 asp 5 的 project.json 文件以包含对包装程序集的引用,如下所示:

Finally, update the project.json file of your asp 5 to include a reference to your wrapped assembly as in:

"frameworks": {
  "dnx451": {
    "dependencies": {
      "Foo": "1.0.0-*",
    }
  }
},

  • 或者,使用 VS.右键单击 ASP 5 项目中的引用,选择 Add Reference...,然后单击 Browse.现在导航到包含程序集的文件夹并选择 dll 文件.

  • Alternatively, use VS. Right click references in your ASP 5 project, select Add Reference... and then click on Browse. Now navigate to the folder containing the assembly and select the dll file.

    Visual Studio 将为您更新 wrap 文件夹(创建 mySolutionwrapFooproject.json),甚至会在您的 project.json 中添加依赖项(您的 asp 5项目).

    Visual studio will update for you the wrap folder (Creating the mySolutionwrapFooproject.json) and will even add the dependency in your project.json (The one in your asp 5 project).

    当您想在解决方案中添加对非 asp 5 项目的引用时:

    • 使用dnu wrap 命令添加引用.

    • Use the dnu wrap command to add the reference.

    该过程与之前添加对现有程序集的引用的场景非常相似.现在,如果您想在解决方案中添加对非 asp 5 项目的引用,您将运行以下命令:

    The process is quite similar to the previous scenario of adding a reference to an existing assembly. Now if you want to add a reference to a non asp 5 project within your solution, you will run the following command:

    dnu wrap ".myNonAsp5ProjectMyNonAsp5Project.csproj"
    

    当添加对现有程序集的引用时,这将生成/更新 asp 5 解决方案文件夹中的 wrap 文件夹,尽管这次文件 mySolutionwrapMyNonAsp5Projectproject.json 略有不同,因为它包装的是项目而不是程序集:

    As when adding a reference to an existing assembly, this will generate/update the wrap folder inside your asp 5 solution folder, although this time the file mySolutionwrapMyNonAsp5Projectproject.json is slightly different since it is wrapping a project and not an assembly:

    {
      "version": "1.0.0-*",
      "frameworks": {
        "net451": {
          "wrappedProject": "../../MyNonAsp5Project/MyNonAsp5Project.csproj",
          "bin": {
            "assembly": "../../MyNonAsp5Project/obj/{configuration}/MyNonAsp5Project.dll",
            "pdb": "../../MyNonAsp5Project/obj/{configuration}/MyNonAsp5Project.pdb"
          }
        }
      }
    }
    

    同样,您需要手动更新 asp 5 的 project.json 文件以包含对包装程序集的引用,如下所示:

    Again, you will need to manually update the project.json file of your asp 5 to include a reference to your wrapped assembly as in:

    "frameworks": {
      "dnx451": {
        "dependencies": {
          "MyNonAsp5Project": "1.0.0-*",
        }
      }
    },
    

  • 或者,使用 VS.右键单击 ASP 5 项目中的引用,选择添加引用....现在展开左侧的Projects,选择项目并点击确定.

  • Alternatively, use VS. Right click references in your ASP 5 project, select Add Reference.... Now expand Projects on the left hand side, select the project and click ok.

    Visual Studio 将更新 wrap 文件夹(创建 mySolutionwrapMyNonAsp5Projectproject.json),甚至会在您的 project.json(您的 asp 5 项目中的那个)中添加依赖项.

    Visual studio will update the wrap folder (Creating mySolutionwrapMyNonAsp5Projectproject.json) and will even add the dependency in your project.json (The one in your asp 5 project).

    附注.最近我一直在升级到 Win10 并安装/卸载东西,不知何故我最终得到了一个环境变量 Platform=MCD.这将被 MSBuild 用作默认平台,可能会给您带来一些痛苦.具体来说,我在运行 dnu wrap 命令来包装 csproj 文件时遇到错误 无法解析引用.(作为内部,它使用 msbuild 来解析 csproj参考).

    PS. I have been upgrading to Win10 and installing/uninstalling stuff as of lately and somehow I ended up with an environment variable Platform=MCD. This will be taken by MSBuild as the default platform and may give you some pain. Specifically, I was getting the error Failed to resolve references when running the dnu wrap command for wrapping a csproj file. (As internally it uses msbuild to resolve the csproj references).

    这篇关于如何在 MVC6 中使用标准类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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