How to return HTTP 500 from ASP.NET Core RC2 Web Api?

Back in RC1, I would do this: [HttpPost] public IActionResult Post([FromBody]string something) { try{ // … } catch(Exception e) { return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError); } } In RC2, there no longer is HttpStatusCodeResult, and there is nothing I can find that lets me return a 500 type of IActionResult. Is the approach now entirely different for … Read more

ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response

Sending a form POST HTTP request (Content-Type: application/x-www-form-urlencoded) to the below controller results into a HTTP 415 Unsupported Media Type response. public class MyController : Controller { [HttpPost] public async Task<IActionResult> Submit([FromBody] MyModel model) { //… } } Form post HTTP headers: POST /submit HTTP/1.1 Host: example.com:1337 Connection: keep-alive Content-Length: 219 Pragma: no-cache Cache-Control: no-cache … Read more

Getting value from appsettings.json in .net core

Not sure what am I missing here but I am not able to get the values from my appsettings.json in my .net core application. I have my appsettings.json as: { “AppSettings”: { “Version”: “One” } } Startup: public class Startup { private IConfigurationRoot _configuration; public Startup(IHostingEnvironment env) { _configuration = new ConfigurationBuilder() } public void … Read more

ASP.NET Core Get Json Array using IConfiguration

In appsettings.json { “MyArray”: [ “str1”, “str2”, “str3” ] } In Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IConfiguration>(Configuration); } In HomeController public class HomeController : Controller { private readonly IConfiguration _config; public HomeController(IConfiguration config) { this._config = config; } public IActionResult Index() { return Json(_config.GetSection(“MyArray”)); } } There are my codes above, I got null … Read more

How to get the Development/Staging/production Hosting Environment in ConfigureServices

How do I get the Development/Staging/production Hosting Environment in the ConfigureServices method in Startup? public void ConfigureServices(IServiceCollection services) { // Which environment are we running under? } The ConfigureServices method only takes a single IServiceCollection parameter. 15 Answers 15

ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate

I created an .NET Core MVC application and use Dependency Injection and Repository Pattern to inject a repository to my controller. However, I am getting an error: InvalidOperationException: Unable to resolve service for type ‘WebApplication1.Data.BloggerRepository’ while attempting to activate ‘WebApplication1.Controllers.BlogController’. Model (Blog.cs) namespace WebApplication1.Models { public class Blog { public int BlogId { get; set; … Read more