Tag: asp.net core

如果MSB3277出现在asp net core app中该怎么办

我正在使用引用EntityFrameworkCore 2.1.3包的Database-Project,但AspNetCore.App-metapackage包含EntityFrameworkCore 2.1.2。 引入我的数据库项目后,我得到这个msbuild警告: Warning MSB3277 Found conflicts between different versions of “Microsoft.EntityFrameworkCore” that could not be resolved. 我搜索了一下,我找到了这个文档: https ://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.1 我使用的第三方库(NpgSql for Postgre)依赖于2.1.3 …我应该降级所有软件包,以便他们使用efcore 2.1.2软件包还是应该忽略这个警告? 或者更好的问题:哪种方式不那么痛苦? 提前致谢

仅在调用控制器操作时才知道数据库名称时注入DbContext

我有一个WebAPI / EF Core后端应用程序,有几个可能的客户端数据库连接到(每个相同的模式,但一次只有一个)。 在访问WebAPI控制器之前,不知道要连接到哪个数据库。 到那时,使用对starup.cs文件的ConfigureServices()方法中的services.AddDbContext ()的调用,将DBContext添加为可注入对象为时已晚。 由于每个请求都隔离了DbContext的每个实例,直接从控制器的操作实例化DbContext是否有任何问题,而不是通过使用DI传递它?

SendGrid在发送电子邮件时抛出InvalidApiRequestException

SendGrid在发送电子邮件时抛出InvalidApiRequestException 。 我正在使用此代码: public Task SendEmailAsync(string email, string subject, string message) { // Plug in your email service here to send an email. var myMessage = new SendGrid.SendGridMessage(); myMessage.AddTo(email); myMessage.From = new MailAddress(“varshney@shobhit.com”, “Shobhit”, System.Text.Encoding.Default); myMessage.Subject = subject; myMessage.Text = message; myMessage.Html = message; var credentials = new NetworkCredential( Options.SendGridUser, Options.SendGridKey); // Create a Web […]

在视图中调用控制器方法

我在Visual Studio中有一个asp.net核心应用程序。 在自动创建的视图(Events / index.cshtml)中,我想从EventsController.cs中调用此方法 public Boolean IsInRole(string Role) { Boolean roleMembership = false; if (HttpContext.Session.GetInt32(“ID”) != null) { if (HttpContext.Session.GetString(“Role”) == Role) { roleMembership = true; } } return roleMembership; } 我的想法是在视图的顶部调用此方法 @if(IsInRole(“Admin”)) { show some content } 我怎样才能完成这项任务?

从类库项目中访问appsettings.json文件设置

我知道如何在web项目上设置appsettings.json文件。但是你能告诉我如何从类库项目中访问它吗? 我可以在web项目中按如下所示对其进行配置。那么如何从类库项目中访问这些值? 那可能吗 ? public class MyConfig { public string ApplicationName { get; set; } public int Version { get; set; } } public class Startup { public IConfigurationRoot Configuration { get; set; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) Configuration = builder.Build(); } } public […]

将httpHandler附加到httpclientFactory webapi aspnetcore 2.1

我试图使用“ConfigurePrimaryHttpMessageHandler”将处理程序附加到httpclientfactory 但当我查看HttpClient内部以查看处理程序是否存在时,我无法找到它 我是否正确附加了处理程序? 任何建议 services.AddHttpClient(client => { client.BaseAddress = new Uri(myUri); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); }) .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip }); public interface IGitHubClient { Task GetData(); } public class GitHubClient : IGitHubClient { private readonly HttpClient _client; public GitHubClient(HttpClient httpClient) { _client = httpClient; } public async […]

通过托管环境进行自托管

我试图通过weblistener运行ASP.NET Core(.NET 4.6.1)应用程序作为自托管 我添加了一个命令: “commands”: { “web”: “Microsoft.AspNet.Hosting –server Microsoft.AspNet.Server.WebListener –server.urls http://localhost:5000” }, 和project.json必要依赖项 当我通过Visual Studio启动我的应用程序时它工作正常 src\ProjectName\bin\Release\net461\win7-x64\ProjectName.exe ProjectName.exe作为Web服务器启动,具有Hosting environment: Development 但是,当我手动运行它时, ProjectName.exe与Hosting environment: Production一起运行Hosting environment: Production并且它无法正常运行。 首先,我想找到用Development environment手动运行应用程序的Development environment 在VS我有这个选项:

Asp.net核心 – 没有这样的表:AspNetUsers

所以我试图为我的asp.net核心应用程序实现用户登录。 我在这里关注微软教程。 我有两个上下文,一个叫做SchoolContext,用于保存所有与学校相关的模型,另一个上下文叫做ApplicationDbContext,用于Account模型。 这都被保存到sqlite数据库。 一切正常,直到我尝试将用户注册到我的上下文。 当我尝试注册我得到的用户时,无法找到AspNetUsers表错误。 如果我查看数据库,我看不到AspNetUser表。 我尝试添加迁移 ,但我仍然得到同样的错误。 为什么没有创建表? Startup.cs public class Startup { public IConfigurationRoot Configuration { get; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) .AddJsonFile($”appsettings.{env.EnvironmentName}.json”, optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } // This method gets called by the runtime. Use this method […]

我应该等待ValueTask 吗?

哪个是ValueTask的有效实现? 缓存服务从缓存或DB返回数据。 public async ValueTask<IList> GetEmployeesFacts() { try { var facts = (List) _memoryCache.Get(“facts”); return facts ?? await _accountService.GetEmploymentFacts(DetailsRequestType.All, null); } catch (Exception e) { var tc = new TelemetryClient(); tc.TrackException(e); return null; } } 这是: var employeesFacts = await _cacheService.GetEmployeesFacts(); 或var employeesFacts = _cacheService.GetEmployeesFacts().Result; 这里有点困惑。

使用在startup.cs中分配的委托处理OnTokenValidated的问题

我想在ASP.NET Core 2.0中正确使用DI,以便让我的自定义方法处理在身份validation期间validationJWT令牌后触发的OnTokenValidated事件。 下面的解决方案有效, 除了在处理程序中我使用一个注入的服务来命中MemoryCache来检查控制器中其他位置添加的缓存项(我已经validation它们已被添加和保留),并且当它被访问时,缓存是总是空的。 我怀疑这是因为我的自定义处理程序对象是由不同的容器创建的(由于早期的BuildServiceProvider()调用?)并且正在使用MemoryCache (或类似的)的单独实例。 如果是这种情况,我想我不清楚如何在startup.cs中的ConfigureServices()中正确添加和引用我的类和方法,以便它按预期工作。 这就是我所拥有的: public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); … services.AddScoped(); // add other services … var sp = services.BuildServiceProvider(); services.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, bOptions => { // Configure JwtBearerOptions bOptions.Events = new JwtBearerEvents { OnTokenValidated = sp.GetService().JwtTokenValidated }; } 我的自定义处理程序类如下。 ValidateSessionAsync()调用使用注入的AppSessionService访问MemoryCache对象并确保存在缓存条目: public class JwtTokenValidatedHandler : IJwtTokenValidatedHandler { AppSessionService _session; public JwtTokenValidatedHandler(AppSessionService […]