Tag: asp.net core

在ASP.NET5控制台应用程序中使用Startup类

ASP.NET 5-beta4控制台应用程序(从VS2015中的ASP.NET控制台项目模板构建)是否可以使用Startup类来处理注册服务和设置配置详细信息? 我试图创建一个典型的Startup类,但是在通过dnx . run运行控制台应用程序时似乎永远不会调用它dnx . run dnx . run或在Visual Studio 2015中。 Startup.cs几乎是: public class Startup { public Startup(IHostingEnvironment env) { Configuration configuration = new Configuration(); configuration.AddJsonFile(“config.json”); configuration.AddJsonFile(“config.{env.EnvironmentName.ToLower()}.json”, optional: true); configuration.AddEnvironmentVariables(); this.Configuration = configuration; } public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSubKey(“Settings”)); services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(this.Configuration[“Data:DefaultConnection:ConnectionString”])); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) […]

升级到ASP.NET Core 2.0后,无法从singleton IActiveUsersService使用作用域服务IMongoDbContext

我今天更新了一个项目到ASP.NET Core 2,我收到以下错误: 无法从singleton IActiveUsersService使用作用域服务IMongoDbContext 我有以下注册: services.AddSingleton(); services.AddScoped(); services.AddSingleton(option => { var client = new MongoClient(MongoConnectionString.Settings); return client.GetDatabase(MongoConnectionString.Database); }) public class MongoDbContext : IMongoDbContext { private readonly IMongoDatabase _database; public MongoDbContext(IMongoDatabase database) { _database = database; } public IMongoCollection GetCollection() where T : Entity, new() { return _database.GetCollection(new T().CollectionName); } } public class IActiveUsersService: ActiveUsersService […]

entity framework核心 – 将小数精度和比例设置为所有小数属性

我想将所有十进制属性的精度设置为(18,6)。 在EF6中,这非常简单: modelBuilder.Properties().Configure(x => x.HasPrecision(18, 6)); 但我似乎无法在EF Core中找到类似的东西。 删除级联删除约定并不像在EF6中那么简单,因此我找到了以下解决方法: EF6: modelBuilder.Conventions.Remove(); modelBuilder.Conventions.Remove(); EF核心: foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) relationship.DeleteBehavior = DeleteBehavior.Restrict; 在我读完之后,我尝试了类似的方法: foreach (var entityType in modelBuilder.Model.GetEntityTypes() .SelectMany(x => x.GetProperties()) .Where(x => x.ClrType == typeof(decimal))) { // what to do here? } 我想如果我在正确的轨道上以及如何继续,或者如果没有,我应该开始在所有decimal属性上放置数据注释。

在ASP.NET Core中,如何检查请求是否是本地的?

在常规ASP.NET中,您可以在视图中执行此操作以确定当前请求是否来自localhost: HttpContext.Current.Request.IsLocal 但我无法在ASP.NET 6 / Core /中找到类似的内容。

在MVC控制器内打开websocket通道

有没有人在MVC控制器内打开websocket连接有任何经验? 技术堆栈: ASPNET Core 1.0(RC1)MVC,dnx46,System.Net.WebSockets 为什么MVC而不是中间件:为了整体一致性,路由,已经注入的存储库,在同一个控制器中调用私有方法的选项。 [HttpGet(“v1/resources/{id}”)] public async Task GetAsync(string id) { var resource = await this.repository.GetAsync(id); if (resource == null) { return new HttpStatusCodeResult(404); } if (this.HttpContext.WebSockets.IsWebSocketRequest) { var webSocket = await this.HttpContext.WebSockets.AcceptWebSocketAsync(); if (webSocket != null && webSocket.State == WebSocketState.Open) { while (true) { var response = string.Format(“Hello! Time {0}”, System.DateTime.Now.ToString()); var […]

如何在DNX Core 5.0(ASP.NET 5)中引用执行程序集?

我正在移植.NET 3.5 – 4.5中的一些代码。 在我的程序集内部,我有一些代码从当前正在执行的程序集中读取资源。 但是, GetExecutingAssembly()不是DNX核心5.0中的Assembly类型的方法。 var xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(xsdPath); DNX核心5.0中的Assembly.GetExecutingAssembly()相当于什么? 或者,如果我需要一个命名空间来获取该方法(也许是一个扩展方法?),那么命名空间是什么?

ASP.NET Core“CreatedAtRoute”失败

在我的ASP.NET Core应用程序中,我有一个像这样的控制器动作: [HttpPost] public async Task CreateSubscriber([FromBody] SubscriberDef subscriber) { //…implementation removed var link = Url.Link(“SubscriberLink”, new { id = subscriber.ID }); return Created(link, null); } 上面的代码按预期工作。 但是,如果我使用内置方法“CreatedAtRoute”,那么我得到一个例外: [HttpPost] public async Task CreateSubscriber([FromBody] SubscriberDef subscriber) { //…implementation removed return CreatedAtRoute(“SubscriberLink”, new { id = subscriber.ID }); } 例外是: System.InvalidOperationException:没有路由与提供的值匹配。 该exception导致服务返回500状态代码。 在任何一种情况下它都是相同的路由,所以我不知道为什么第一个例子正常工作而第二个例子没有。 我的project.json包含这个: “frameworks”: { “dnx46”: […]

从ASP.NET核心中的类库加载和注册API控制器

我正在使用ASP.NET Core 1.0.1。 我有以下内容 使用”Microsoft.AspNetCore.Mvc”: “1.0.1”类库,以便开发我的控制器: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace CoreAPIsLibrary.Controllers { [Route(“api/[controller]”)] public class ValuesContoller : Controller { public string Get() { return “value”; } // GET api/values/5 [HttpGet(“{id}”)] public string Get(int id) { return “value”; } // POST api/values [HttpPost] public void Post([FromBody]string value) { } […]

如何使用UserManager在IdentityUser上加载导航属性

我已经扩展了IdentityUser以包含用户地址的导航属性,但是当使用UserManager.FindByEmailAsync获取用户时,不会填充导航属性。 ASP.NET Identity Core是否有某种方法可以填充Entity Framework的Include()等导航属性,还是必须手动执行? 我已经设置了这样的导航属性: public class MyUser : IdentityUser { public int? AddressId { get; set; } [ForeignKey(nameof(AddressId))] public virtual Address Address { get; set; } } public class Address { [Key] public int Id { get; set; } public string Street { get; set; } public string Town { get; set; } […]

Asp.Net Core 2.0 Kestrel不提供静态内容

编辑:添加了红隼 使用IIS express运行Asp.Net Core 2.0应用程序时,静态文件(css,js)按预期提供。 但是,当通过“dotnet publish -o [targetDirectory]”和dotnet [website.dll]使用命令行/ Kestrel时,Kestrel不会提供任何静态内容。 利用浏览器F12,我看到Kestrel返回404错误。 仔细观察,当直接在浏览器中输入文件路径(localhost:5000 / css /cssfile.css)时,文件不会显示,但浏览器似乎重定向到“localhost:5000 / cssfile.css”,但仍然返回404错误(注意缺少的/ css /目录)。 我通过Visual Studio 2017创建了这个项目,并为新的MVC Core 2.0应用程序选择了默认值(安装了SDK)。 我已按照此处的步骤在 program.cs和startup.cs文件中启用静态文件。 这些实现“app.UseStaticFiles();” 和“.UseContentRoot(Directory.GetCurrentDirectory())”。 通过谷歌找到的文章似乎都没有帮助。 我已经validation了dotnet将静态内容复制到目标目录。 我错过了什么? 谢谢 // Program.cs public static IWebHost BuildWebHost(string[] args) => WebHost .CreateDefaultBuilder(args) .UseStartup() .Build(); // Startup.cs public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseExceptionHandler(“/Error/HandleError”); […]