How to return a specific status code and no contents from Controller?

this.HttpContext.Response.StatusCode = 418; // I’m a teapot How to end the request? Try other solution, just: return StatusCode(418); You could use StatusCode(???) to return any HTTP status code. Also, you can use dedicated results: Success: return Ok() ← Http status code 200 return Created() ← Http status code 201 return NoContent(); ← Http status code … Read more

How do I access Configuration in any class in ASP.NET Core?

Update Using ASP.NET Core 2.0 will automatically add the IConfiguration instance of your application in the dependency injection container. This also works in conjunction with ConfigureAppConfiguration on the WebHostBuilder. For example: public static void Main(string[] args) { var host = WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(builder => { builder.AddIniFile(“foo.ini”); }) .UseStartup<Startup>() .Build(); host.Run(); } It’s just as easy as … Read more

Publish to IIS, setting Environment Variable

This answer was originally written for ASP.NET Core RC1. In RC2 ASP.NET Core moved from generic httpPlafrom handler to aspnetCore specific one. Note that step 3 depends on what version of ASP.NET Core you are using. Turns out environment variables for ASP.NET Core projects can be set without having to set environment variables for user … Read more

.NET Core MVC Page Not Refreshing After Changes

In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange is not available. Surprised that refreshing a view while the app is running did not work, I discovered the following solution: Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project Add the following in Startup.cs: services.AddControllersWithViews().AddRazorRuntimeCompilation(); Here’s the full explanation for the curious.

How to get the current logged in user ID in ASP.NET Core?

Update in ASP.NET Core Version >= 2.0 In the Controller: public class YourControllerNameController : Controller { private readonly UserManager<ApplicationUser> _userManager; public YourControllerNameController(UserManager<ApplicationUser> userManager) { _userManager = userManager; } public async Task<IActionResult> YourMethodName() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) // will give the user’s userId var userName = User.FindFirstValue(ClaimTypes.Name) // will give the user’s userName // For … Read more

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

From what I can see there are helper methods inside the ControllerBase class. Just use the StatusCode method: [HttpPost] public IActionResult Post([FromBody] string something) { //… try { DoSomething(); } catch(Exception e) { LogException(e); return StatusCode(500); } } You may also use the StatusCode(int statusCode, object value) overload which also negotiates the content.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)