cur*_*wag 75 c# asp.net asp.net-mvc json
我正在尝试为我正在构建的.Net Web应用程序设置应用程序基本路径.我一直在配置构建器上遇到错误.这是我得到的错误.
DNX,Version=v4.5.1 error CS1061: 'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)
我假设我会得到同样的错误我.AddJsonFile()和.AddEnvironmentVariables().我做错什么了吗?我没有为project.json添加正确的依赖项吗?我附上了我的startup.cs和我的project.json.
project.json
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"tooling": {
"defaultNamespace": "TripPlanner"
},
"dependencies": {
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.Framework.Configuration": "1.0.0-beta8",
"Microsoft.Framework.DependencyInjection": "1.0.0-beta8"
//"Microsoft.Extensions.PlatformAbstractions": "1.0.0-beta8"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}
Run Code Online (Sandbox Code Playgroud)
startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using TripPlanner.Services;
namespace TripPlanner
{
public class Startup
{
public static IConfigurationRoot Configuration;
public Startup(IApplicationEnvironment appEnv){
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services)
{
services.AddMvc();
#if DEBUG
services.AddScoped<IMailService, DebugMailService> ();
#else
services.AddScoped<IMailService, RealMailService> ();
#endif
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
//app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(config =>
{
config.MapRoute(
name: "Default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "App", action = "Index"}
);
});
}
// Entry point for the application.
public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}
}
Run Code Online (Sandbox Code Playgroud)
错误发生在public startupstartup.cs顶部附近的函数中.
小智 141
我能够解决这个问题.如果您还没有解决它,请在project.json中尝试以下操作.添加以下内容:
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final"
Run Code Online (Sandbox Code Playgroud)
它应该工作
小智 33
不确定是否还有人遇到此问题,但我能够通过以下方式在dotnetcore控制台项目(netcoreapp2.0)中解决此问题:
dotnet add package Microsoft.Extensions.Configuration.Json
Run Code Online (Sandbox Code Playgroud)
小智 9
尝试在您的.csproj文件中添加以下内容:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
还有一个可能的问题...
我必须添加这个:
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.0.0" />
Run Code Online (Sandbox Code Playgroud)
为了编译:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
...
Run Code Online (Sandbox Code Playgroud)
我很确定这是一个控制台应用程序。
其他需要考虑的事情:
using Microsoft.Extensions.Configuration;
Run Code Online (Sandbox Code Playgroud)
如果没有该“using”语句,它将在 Microsoft.Extensions.Configuration.FileExtensions 中找不到扩展方法。
我遇到了麻烦,因为我们还:
using System.Configuration;
Run Code Online (Sandbox Code Playgroud)
并且与“ConfigurationBuilder”发生了名称冲突。添加
using Microsoft.Extensions.Configuration;
Run Code Online (Sandbox Code Playgroud)
行...删除
using System.Configuration;
Run Code Online (Sandbox Code Playgroud)
行,然后完全限定 System.Configuration 下的任何内容。
| 归档时间: |
|
| 查看次数: |
35850 次 |
| 最近记录: |