asp.net-core-mvc

Using Asp.Net Core 2 Injection for Serilog with Multiple Projects

孤者浪人 提交于 2020-06-09 10:26:28
问题 I have Serilog configured for Asp.Net Core 2.0 and it works great via .Net Core dependency injection in my startup web project (if I use it through Microsoft.Extensions.Logging), but I can't access it from any other project. Here's what I have: Program.cs using System; using System.IO; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Serilog; namespace ObApp.Web.Mvc { public class Program { public static IConfiguration

Manual controller registration in ASP.NET Core dependency injection

落爺英雄遲暮 提交于 2020-06-08 16:39:21
问题 I need to override ASP.NET Core's default registration for a certain controller. I've tried the below, but it resolves MyController from the automatic registration. services.AddTransient((provider) => new MyController(...)); How can I override this? 回答1: By default, controllers are resolved using type activation , which boils down to the framework using an equivalent of Activator.CreateInstance to create controller instances. The dependencies for these controllers are sourced from the DI

Manual controller registration in ASP.NET Core dependency injection

守給你的承諾、 提交于 2020-06-08 16:38:09
问题 I need to override ASP.NET Core's default registration for a certain controller. I've tried the below, but it resolves MyController from the automatic registration. services.AddTransient((provider) => new MyController(...)); How can I override this? 回答1: By default, controllers are resolved using type activation , which boils down to the framework using an equivalent of Activator.CreateInstance to create controller instances. The dependencies for these controllers are sourced from the DI

Manual controller registration in ASP.NET Core dependency injection

一世执手 提交于 2020-06-08 16:37:50
问题 I need to override ASP.NET Core's default registration for a certain controller. I've tried the below, but it resolves MyController from the automatic registration. services.AddTransient((provider) => new MyController(...)); How can I override this? 回答1: By default, controllers are resolved using type activation , which boils down to the framework using an equivalent of Activator.CreateInstance to create controller instances. The dependencies for these controllers are sourced from the DI

NotFound() returns 200 instead of 404

早过忘川 提交于 2020-06-03 06:20:14
问题 I've been stuck on this recently and can't figure out why this is happening. I'm using an MVC Controller in .Net Core to return a NotFound() "404" response. However, client side (using angular) if I console.log the response, it shows this... status:200 statusText:"OK" Is there any reason why returning NotFound() would return an error code of 200 instead of the intended 404? This is my Controller GET. // GET: api/cause/cause-name [HttpGet("{name}")] [AllowAnonymous] public IActionResult

NotFound() returns 200 instead of 404

主宰稳场 提交于 2020-06-03 06:19:59
问题 I've been stuck on this recently and can't figure out why this is happening. I'm using an MVC Controller in .Net Core to return a NotFound() "404" response. However, client side (using angular) if I console.log the response, it shows this... status:200 statusText:"OK" Is there any reason why returning NotFound() would return an error code of 200 instead of the intended 404? This is my Controller GET. // GET: api/cause/cause-name [HttpGet("{name}")] [AllowAnonymous] public IActionResult

NotFound() doesn't seem to work as expected

自闭症网瘾萝莉.ら 提交于 2020-06-01 05:06:20
问题 I'm new to ASP.NET core, so I hope you bear with me on this one. This is similar to this unanswered question. When I'm testing a brand new C# MVC project, and I enter a wrong URL, there's no info. Just a blank page. To solve this, I have adapted startup.cs adding a UseStatusCodePagesWithReExecute() to return a static 404.html page. This works. So far, so good. Now, I am coding a basic login logic. For testing purposes, I'm calling return NotFound(); when the post parameters are missing. This

Model binding of nested properties in asp.net core 2.2

我只是一个虾纸丫 提交于 2020-05-30 05:17:25
问题 I'm trying to create a common complex object for my models (action parameters) and reuse it in many places. Here is some sample code: [HttpGet("/api/values")] public ActionResult<string> Get([FromQuery] MyModel model) { var sb = new StringBuilder(); sb.AppendLine(model.Id); sb.AppendLine($"{model.Id}-{model.Generated?.DateStart}-{model.Generated?.DateEnd}"); sb.AppendLine($"{model.Id}-{model.Reference?.DateStart}-{model.Reference?.DateEnd}"); return sb.ToString(); } public class MyModel {

Model binding of nested properties in asp.net core 2.2

女生的网名这么多〃 提交于 2020-05-30 05:16:10
问题 I'm trying to create a common complex object for my models (action parameters) and reuse it in many places. Here is some sample code: [HttpGet("/api/values")] public ActionResult<string> Get([FromQuery] MyModel model) { var sb = new StringBuilder(); sb.AppendLine(model.Id); sb.AppendLine($"{model.Id}-{model.Generated?.DateStart}-{model.Generated?.DateEnd}"); sb.AppendLine($"{model.Id}-{model.Reference?.DateStart}-{model.Reference?.DateEnd}"); return sb.ToString(); } public class MyModel {

.NET Core Web API: multiple [FromBody]?

时光毁灭记忆、已成空白 提交于 2020-05-29 07:43:30
问题 I am converting code that was written in ASP.NET MVC to ASP.NET Core MVC. While I was converting the code, I encountered a problem. We used a method that has multiple parameters like this: [HttpPost] public class Search(List<int> ids, SearchEntity searchEntity) { //ASP.NET MVC } But when coding this in .NET Core, the ids parameter is null . [HttpPost] public class Search([FromBody]List<int> ids,[FromBody]SearchEntity searchEntity) { //ASP.NET Core MVC } When I place the ids parameter in the