Tag: asp.net core

如何确保将appsettings.dev.json复制到输出文件夹?

我有三个配置文件,每个环境一个: appsettings.json – >制作 appsettings.dev.json – >开发 appsettings.stg.json – > staging 如果我将ASPNETCORE_ENVIRONMENT设置为dev ,我会收到一个运行时exception,抱怨无法找到appsettings.dev.json。 我尝试添加 “copyToOutput”: [ “appsettings.dev.json” ] 到project.json中的buildOptions部分,但似乎没有任何效果。 有没有其他方法可以强制appsettings.dev.json复制到输出目录?

.NET核心entity framework – 在类库中为Context添加迁移

我在向.NET Core类库中的Entity Framework数据库上下文添加初始迁移时遇到问题。 当我跑: dotnet ef migrations add migrationName -c PlaceholderContext 我收到错误: Could not invoke this command on the startup project ‘Placeholder.Data’. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds. 所以我点击了链接 […]

.NET Core 2.1 Identity为所有用户提供相关角色

我正在尝试为用户管理管理页面提取所有Identity用户及其相关角色。 我认为这将相当容易,但显然不是。 我尝试过以下解决方案: https : //stackoverflow.com/a/43562544/5392786但到目前为止还没有解决。 这是我到目前为止: ApplicationUser: public class ApplicationUser : IdentityUser { public List<IdentityUserRole> Roles { get; set; } } 的DbContext public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } } 启动标识代码 services.AddIdentity(options => options.Stores.MaxLengthForKeys = 128) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); 我要显示列表的Razor页面: public class IndexModel : PageModel { private readonly UserManager […]

Post FromBody始终无效

我有一个新的API,我正在使用ASP.NET Core构建,我无法将任何数据POST到端点。 这是端点的样子: [HttpPost] [Route(“StudentResults”)] public async Task GetStudentResults([FromBody]List userSocs, [FromBody]int collegeId) { var college = await _collegeService.GetCollegeByID(collegeId); // var occupations = await _laborMarketService.GetOccupationProgramsBySocsAndCollege(userSocs, college); return Ok(); } 这就是我通过Postman发送的有效载荷如下所示: { “userSocs”: [ “291123”, “291171”, “312021”, “291071”, “152031”, “533011” ], “collegeId”: 1 } 我确保我将postman设置为POST,使用Content-Type application/json 。 我究竟做错了什么?

在ASP.NET Core 2中dependency injection相同类型的多个实例

在ASP.NET Core 2 Web Api中,我想使用dependency injection将HttpClient httpClientA实例注入ControllerA ,并将HttpClient的实例httpClientB ControllerB 。 DI注册码看起来像: HttpClient httpClientA = new HttpClient(); httpClientA.BaseAddress = endPointA; services.AddSingleton(httpClientA); HttpClient httpClientB = new HttpClient(); httpClientB.BaseAddress = endPointB; services.AddSingleton(httpClientB); 我知道我可以将HttpClient子类HttpClient每个控制器创建一个唯一类型,但这不能很好地扩展。 什么是更好的方法? 更新特别针对HttpClient,微软似乎正在开发一些工作 https://github.com/aspnet/HttpClientFactory/blob/dev/samples/HttpClientFactorySample/Program.cs#L32 – 感谢@ mountain-traveler(Dylan)指出这一点。

asp.net核心中的TempData null

我试图在asp.net核心中使用TempData但是我在TempData的get方法上得到一个null值。 任何人都可以告诉我如何在asp.net核心中使用TempData 以下是我根据研究添加的内容。 Project.json文件 { “dependencies”: { “Microsoft.NETCore.App”: { “version”: “1.0.1”, “type”: “platform” }, “Microsoft.AspNetCore.Mvc”: “1.0.1”, “Microsoft.AspNetCore.Routing”: “1.0.1”, “Microsoft.AspNetCore.Server.IISIntegration”: “1.0.0”, “Microsoft.AspNetCore.Server.Kestrel”: “1.0.1”, “Microsoft.Extensions.Configuration.EnvironmentVariables”: “1.0.0”, “Microsoft.Extensions.Configuration.FileExtensions”: “1.0.0”, “Microsoft.Extensions.Configuration.Json”: “1.0.0”, “Microsoft.Extensions.Logging”: “1.1.0”, “Microsoft.Extensions.Logging.Console”: “1.0.0”, “Microsoft.Extensions.Logging.Debug”: “1.0.0”, “Microsoft.Extensions.Options.ConfigurationExtensions”: “1.0.0”, “Microsoft.EntityFrameworkCore.SqlServer”: “1.1.0”, “Microsoft.EntityFrameworkCore.Tools”: “1.1.0-preview4-final”, “Microsoft.EntityFrameworkCore.Design”: “1.1.0”, “Microsoft.EntityFrameworkCore.SqlServer.Design”: “1.1.0”, “DataBase”: “1.0.0-*”, “UnitOfWork”: “1.0.0-*”, “ViewModel”: “1.0.0-*”, “Common”: “1.0.0-*”, “System.IdentityModel.Tokens.Jwt”: “5.0.0”, “Microsoft.AspNetCore.Authentication.JwtBearer”: […]

使用特定的HttpMessageHandler注入单实例HttpClient

作为我正在开发的ASP.Net核心项目的一部分,我需要从WebApi中与许多不同的基于Rest的API端点进行通信。 为了实现这一点,我使用了许多服务类,每个服务类都实例化一个静态HttpClient 。 基本上,我为WebApi连接的每个基于Rest的端点都有一个服务类。 下面是一个如何在每个服务类中实例化静态HttpClient的示例。 private static HttpClient _client = new HttpClient() { BaseAddress = new Uri(“http://endpointurlexample”), }; 虽然上述方法运行良好,但它不允许对使用HttpClient的服务类进行有效的unit testing。 为了让我能够进行unit testing,我有一个假的HttpMessageHandler ,我想在我的unit testing中用于HttpClient ,而HttpClient是如上所述实例化的,但是我无法将假的HttpMessageHandler作为unit testing的一部分。 HttpClient在服务类中保持整个应用程序中的单个实例(每个端点一个实例)的最佳方法是什么,但允许在unit testing期间应用不同的HttpMessageHandler ? 我想到的一种方法是不使用静态字段来保存服务类中的HttpClient ,而是允许使用单例生命周期通过构造函数注入来注入它,这将允许我使用所需的HttpMessageHandler指定HttpClient在unit testing期间,我想到的另一个选项是使用HttpClient工厂类,它在静态字段中实例化HttpClient ,然后可以通过将HttpClient工厂注入服务类来检索,再次允许使用相关的HttpMessageHandler进行不同的实现在unit testing中返回。 以上都没有感到特别干净,感觉必须有更好的方法吗? 有任何问题,请告诉我。

使用HttpClient,HTTPS请求失败

我使用以下代码并获取HttpRequestExceptionexception: using (var handler = new HttpClientHandler()) { handler.ClientCertificateOptions = ClientCertificateOption.Manual; handler.SslProtocols = SslProtocols.Tls12; handler.ClientCertificates.Add(new X509Certificate2(@”C:\certificates\cert.pfx”)); // I also tried to add another certificates that was provided to https access // by administrators of the site, but it still doesn’t work. //handler.ClientCertificates.Add(new X509Certificate2(@”C:\certificates\cert.crt”)); //handler.ClientCertificates.Add(new X509Certificate2(@”C:\certificates\cert_ca.crt”)); using (var client = new HttpClient(handler)) { var response = […]

如何提取自定义标头值?

我从我的项目中接受的答案中获得了这个确切的代码,我需要将其迁移到ASP.NET Core MVP中。 如何在Web API消息处理程序中提取自定义标头值? var env = Request.Headers.GetValues(“environment”).First(); 我如何在.NET Core中实现它? 我希望这不是一个重复的问题,因为我试图用新系统而不是旧系统来做这件事。 如果有人在关于当前版本的链接上添加答案,我也会没事的。 编辑: 哪些类型的http标头在ASP.NET 5中消失了? 我试过这个链接,但API可能已经改变了。 我不认为这也是这个问题的重复。

ASP.Net核心应用程序中LocalAppData的路径

我有一个ASP.Net核心应用程序,为了目前的目的,我必须使用LocalAppData。 通常我会编写Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) ,但遗憾的是它不能与ASP.Net Core一起使用。 任何人都可以帮我这个吗? UPDATE 感谢Adem Caglin,我找到了解决方案。 我使用的代码: Environment.GetEnvironmentVariable( RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? “LocalAppData” : “Home”);