asp.net-core-2.0

Accessing Session object inside an Asp Core 2 View

烈酒焚心 提交于 2019-12-05 07:58:47
I want to show Session in view. Is that possible? I try with this in my view <div class="content-header col-xs-12"> <h1>Welcome, @HttpContext.Session.GetString("userLoggedName")</h1> </div> But i get an error Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'HttpContext.Session' Any help, i will appreciate it. Thanks You can inject IHttpContextAccessor implementation to your view and use it to get the Session object @using Microsoft.AspNetCore.Http @inject IHttpContextAccessor

Global Error Catcher

那年仲夏 提交于 2019-12-05 07:45:35
In my project I want to log all the errors happen in the application in a database table. I catch almost all errors using try catch blocks, but I cannot catch the global errors like 4xx and 5xx. There are cases that the application does not redirect to the Exception Handler defined in the Configure method of the Startup.cs like when I type a url which does not exist. app.UseExceptionHandler("/Error"); Is there a way to catch all the unhandled errors occurred in my application? You could create your own Exception filter, which implements IExceptionFilter e.g.: public class GlobalExceptionFilter

How to read connection string inside .NET Standard Class library project from ASP.NET Core

孤街醉人 提交于 2019-12-05 07:39:57
问题 In my solution, I have a ASP.NET Core web project and a .NET Standard class library project. Class library project is the data access layer and I want to read the connection string from my appsettings.json (ASP.NET Core project) in my data access layer. I have found few answers such as by Andrii Litvinov which looks like quite straight forward to implement but he also mentioned about implementing through Dependency Injection. I don't want to choose the easy shortcut way but looking for the

getting the request body inside HttpContext from a Middleware in asp.net core 2.0

北慕城南 提交于 2019-12-05 07:22:52
I am having a simple middleware which fetches the body of the request and store it in a string. It is reading fine the stream, but the issue is it wont call my controller which called just after I read the stream and throw the error A non-empty request body is required . Below is my code. public async Task Invoke(HttpContext httpContext) { var timer = Stopwatch.StartNew(); ReadBodyFromHttpContext(httpContext); await _next(httpContext); timer.Stop(); } private string ReadBodyFromHttpContext(HttpContext httpContext) { return await new StreamReader(httpContext.Request.Body).ReadToEndAsync(); }

Swagger default value for parameter

非 Y 不嫁゛ 提交于 2019-12-05 07:06:22
How do I define default value for property in swagger generated from following API? public class SearchQuery { public string OrderBy { get; set; } [DefaultValue(OrderDirection.Descending)] public OrderDirection OrderDirection { get; set; } = OrderDirection.Descending; } public IActionResult SearchPendingCases(SearchQuery queryInput); Swashbuckle generates OrderDirection as required parameter. I would like to be it optional and indicate to client the default value (not sure if swagger supports this). I don't like making the property type nullable. Is there any other option? Ideally using built

ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working

℡╲_俬逩灬. 提交于 2019-12-05 06:16:34
问题 I am trying to create a Web API server using ASP.NET Core 2.0 which uses azure ad v2 endpoint token authorization. I also have an Angular 2 app where the office365 login happens. I get a token from there and then send a simple request to an authorized action in the Web API server. However my token doesn't pass the authorization checks and I get a 401 Unauthorized response. The description provided is: Bearer error="invalid_token", error_description="The signature key was not found" I decoded

Determine port Kestrel binded to

我是研究僧i 提交于 2019-12-05 06:13:46
I'm writing a simple ASP.NET Core service using ASP.NET Core empty ( web ) template. By default, it binds to port 5000 but I would like it to bind to a random available port on the system. I can do so by modifying BuildWebHost to: public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseUrls("http://*:0") // This enables binding to random port .Build(); It binds to a random port but how do I determine from within the application which port I'm listening to? Hosting addresses of ASP.NET Core application could be accessed via

How to enable Application Logs in Azure for Net Core 2 App?

谁都会走 提交于 2019-12-05 06:03:04
I am trying to enable application logs in azure. I have a dummy Net Core 2 App running in an appService in azure. and basically my goal is to see the trace messages in the log stream and in the application log files but I have not found the right way to do this. One of the challenge I have found reading other posts is that they assume a web config in place. The documentation for ASP.NET Core 2.2 is here . Firstly, enable Application Logging and choose the appropriate level: This may be all you need to do to diagnose any problems. But if you want log messages and see them, install the Microsoft

Cannot resolve scoped service Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.IViewBufferScope from root provider

半世苍凉 提交于 2019-12-05 06:02:02
I'm trying to use in my ASP.NET Core 2.0 web app this sample RazorViewEngineEmailTemplates to create an html email body from View. But when I run it and my controller gets an ajax request, I get this error: Cannot resolve scoped service Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.IViewBufferScope from root provider It's probably coming from resolving dependencies in the RazorViewToStringRenderer class but I have no idea how to fix this. ok, the problem was I used renderer from a Singleton service (EmailerService). I changed its registration to Scoped and it all works now: services.AddScoped

BadImageFormatException when migrating from ASP.Net Core 1.1 to 2.0

拈花ヽ惹草 提交于 2019-12-05 04:36:08
I just migrated an ASP.Net Core 1.1 application to the new 2.0 version that was just released. Now I get the following exception: System.BadImageFormatException: 'Could not load file or assembly 'dotnet-aspnet-codegenerator-design' or one of its dependencies. An attempt was made to load a program with an incorrect format.' The exception is thrown on the following line ( AddMvc ): public IServiceProvider ConfigureServices(IServiceCollection services) { ... services.AddMvc(options => { options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline))); }) .AddJsonOptions(options =>