How to get user Browser name ( user-agent ) in Asp.net Core?
I think this was an easy one. Got the answer in Request.Headers[“User-Agent”].ToString()
I think this was an easy one. Got the answer in Request.Headers[“User-Agent”].ToString()
You can use the attribute prefix asp-route- to prefix your route variable names. Example: <a asp-controller=”Product” asp-action=”GetProduct” asp-route-id=”10″> ProductName</a>
1. Click ‘Show All Files’ in Solution Explorer 2. Right-click over ‘wwwroot’ select ‘Exclude From Project’ 3. Right-click over ‘wwwroot’ select ‘Include in Project’
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
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
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
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.
Using the Select Tag helpers to render a SELECT element In your GET action, create an object of your view model, load the EmployeeList collection property and send that to the view. public IActionResult Create() { var vm = new MyViewModel(); vm.EmployeesList = new List<Employee> { new Employee { Id = 1, FullName = “Shyju” … Read more
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
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.