我在一个解决方案中有一些由授权api项目,一个公共项目和一个ASP.net主应用程序项目组成的代码。我已经浏览了相关的project.json文件并删除了显式的beta8引用,然后进行了一些其他必需的更正。例如,在必要时使用Microsoft.Framework.Configuration更改为Microsoft.Extensions.Configuration。
我从链接的潜在副本中了解到,首先要怀疑的是我的各种project.json文件中的引用无效混合。我没有明确地留在任何地方的beta8引用。因此,我不认为相关问题是相同的。
我被困的地方似乎是一个非常基本的东西:
Error CS1061 Build 'IServiceCollection' does not contain a
definition for 'AddMvc' and no extension method 'AddMvc' accepting a first
argument of type 'IServiceCollection' could be found (are you missing a
using directive or an assembly reference?)
我在project.json中引用了Microsoft.AspNet.Mvc,如果我使用Microsoft.AspNet.mvc"它并不重要。在.cs文件中,错误是否仍然存在。我以为你在新的世界秩序中添加引用,只需将它们添加到project.json即可。如果project.json中存在程序集引用,为什么还会出现此错误?
我的project.json:
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-*",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-*",
"Microsoft.AspNet.Mvc": "6.0.0-*",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
"Microsoft.Framework.Logging": "1.0.0-*",
"Microsoft.Framework.Logging.Console": "1.0.0-*",
"Microsoft.Framework.Logging.Debug": "1.0.0-*",
"System.IdentityModel.Tokens": "5.0.0-*",
"MyCompany.Common": "1.0.0-*"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnx451": { }
},
"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}
破解的代码行是AddMvc:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Dnx.Runtime;
using RamSoft.Authorization.Api.Common;
using System.IdentityModel.Tokens;
using Microsoft.AspNet.Authentication.JwtBearer;
using RamSoft.ITConsole.Common;
using Microsoft.Extensions.PlatformAbstractions;
namespace MyCompany.Authorization.Api
{
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
services.AddInstance(_signingCredentials);
services.AddInstance(_jwtBearerOptions);
services.AddMvc(); // Fail.
}
...
}
答案 0 :(得分:0)
您需要更改项目的依赖项。这是我的project.json
"dependencies": {
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview1-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview1-final",
"type": "build"
},
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
"version": "1.0.0-preview1-final",
"type": "build"
}
}
并在startup.cs中添加using Microsoft.Extensions.Configuration;
我们可以使用AddMvc()至少为Core 1.0 rc2
工作