Questions tagged [asp.net-core-middleware]

17585 questions
380

votes
4

answer
129.2k

Views

What is the difference between services.AddTransient, service.AddScoped and service.AddSingleton methods in ASP.NET Core?

I want to implement dependency injection in Asp.Net Core. So after adding this codes to ConfigureServices method, both ways work. What is the difference between services.AddTransient and service.AddScope methods is Asp.Net Core? public void ConfigureServices(IServiceCollection services) { // Add fr...
Elvin Mammadov
73

votes
19

answer
122.7k

Views

Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute

Does any one know how I can specify the Default value for a DateTime property using the System.ComponentModel DefaultValue Attribute? for example I try this: [DefaultValue(typeof(DateTime),DateTime.Now.ToString('yyyy-MM-dd'))] public DateTime DateCreated { get; set; } And it expects the value to be...
REA_ANDREW
369

votes
20

answer
122.6k

Views

Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state

After fresh installation of Visual Studio 2017 I tried to run .NET Core Web project and when trying to run it on Chrome I am getting this error: Unable to start program, An operation is not legal in the current state
Radenko Zec
74

votes
6

answer
121k

Views

ASP.NET Core return JSON with status code

I'm looking for the correct way to return JSON with a HTTP status code in my .NET Core Web API controller. I use to use it like this: public IHttpActionResult GetResourceData() { return this.Content(HttpStatusCode.OK, new { response = 'Hello'}); } This was in a 4.6 MVC application but now with .NET...
Rossco
239

votes
6

answer
118.3k

Views

How do you create a custom AuthorizeAttribute in ASP.NET Core?

I'm trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was possible to override bool AuthorizeCore(HttpContextBase httpContext). But this no longer exists in AuthorizeAttribute. What is the current approach to make a custom AuthorizeAttribute? What I am trying...
jltrem
127

votes
3

answer
115.3k

Views

How to get HttpContext.Current in ASP.NET Core? [duplicate]

This question already has an answer here: Access the current HttpContext in ASP.NET Core 4 answers We are currently rewriting/converting our ASP.NET WebForms application using ASP.NET Core. Trying to avoid re-engineering as much as possible. There is a section where we use HttpContext in a class li...
HaBo
111

votes
6

answer
113.4k

Views

Select Tag Helper in ASP.NET Core MVC

I need some help with the select tag helper in ASP.NET Core. I have a list of employees that I'm trying to bind to a select tag helper. My employees are in a List EmployeesList and selected value will go into EmployeeId property. My view model looks like this: public class MyViewModel { public int E...
Sam
91

votes
11

answer
112.2k

Views

How to read AppSettings values from .json file in ASP.NET Core

I have setup my AppSettings data in appsettings/Config .json like this: { 'AppSettings': { 'token': '1234' } } I have searched online on how to read AppSettings values from .json file, but I could not get anything useful. I tried: var configuration = new Configuration(); var appSettings = configurat...
Oluwafemi
90

votes
11

answer
108.8k

Views

How to read AppSettings values from Config.json in ASP.NET Core

I have got my AppSettings setup in Config.json like this: { 'AppSettings': { 'token': '1234' } } I have searched online on how to read AppSettings values from config.json but I could not get anything useful. I tried: var configuration = new Configuration(); var appSettings = configuration.Get('AppSe...
Oluwafemi
20

votes
4

answer
107.9k

Views

Pass Model To Controller using Jquery/Ajax

I am trying to pass my model to a controller using JQuery/Ajax, I'm not sure how to do this correctly. So far I have tried using Url.Action but the model is blank. Note: none of the duplicate threads on stackoverflow seem to address using ASP.NET 5 MVC 6. View: $('#inpDateCompleted').change(functi...
Reafidy
158

votes
11

answer
100.6k

Views

How to determine if .NET Core is installed

I know that for older versions of .NET, you can determine if a given version is installed by following https://support.microsoft.com/en-us/kb/318785 Is there an official method of determining if .NET Core is installed? (And I don't mean the SDK, I want to check a server without the SDK, to determ...
weloytty
101

votes
27

answer
97.8k

Views

ASP.NET Core 1.0 on IIS error 502.5

I just updated my server (Windows 2012R2) to .Net Core 1.0 RTM Windows Hosting pack from the previous .Net Core 1.0 RC2. My app works on my PC without any issues but the server keeps showing: HTTP Error 502.5 - Process Failure Common causes of this issue: The application process failed to start The...
Vahid Amiri
79

votes
19

answer
94.1k

Views

Property 'map' does not exist on type 'Observable<Response>'

I am trying to call an API from Angular but am getting this error: Property 'map' does not exist on type 'Observable' The answers from this similar question didn't solve my issue: Angular 2 beta.17: Property 'map' does not exist on type 'Observable'. I am using Angular 2.0.0-beta.17.
Malik Kashmiri
147

votes
4

answer
91.5k

Views

Resolving instances with ASP.NET Core DI

How do I manually resolve a type using the ASP.NET Core MVC built-in dependency injection framework? Setting up the container is easy enough: public void ConfigureServices(IServiceCollection services) { // ... services.AddTransient(); } But how can I resolve ISomeService without performing injection...
davenewza
37

votes
5

answer
88.4k

Views

How to pass multiple parameters to a get method in ASP.NET Core

How can I pass in multiple parameters to Get methods in an MVC 6 controller. For example I want to be able to have something like the following. [Route('api/[controller]')] public class PersonController : Controller { public string Get(int id) { } public string Get(string firstName, string lastName)...
mstrand
86

votes
12

answer
85.2k

Views

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

I've done this before with MVC5 using User.Identity.GetUserId() but that doesn't seem to work here. The User.Identity doesnt have the GetUserId() method I am using Microsoft.AspNet.Identity
MRainzo
144

votes
6

answer
83.6k

Views

ASP.NET Core Web API exception handling

I started using ASP.NET Core for my new REST API project after using regular ASP.NET Web API for many years. I don't see a good way to handle exceptions in ASP.NET Core Web API. I tried to implement exception handling filter/attribute: public class ErrorHandlingFilter : ExceptionFilterAttribute { pu...
Andrei
64

votes
7

answer
83.4k

Views

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(IHostingEnvironme...
aman
102

votes
4

answer
81.1k

Views

The default XML namespace of the project must be the MSBuild XML namespace

I cloned the ASP.NET Core SignalR Repo locally, and try opening the solution from within the following environment. IDE Microsoft Visual Studio Enterprise 2015 Version 14.0.25431.01 Update 3 Microsoft .NET Framework Version 4.6.01055 DOT NET CLI λ dotnet --info .NET Command Line Tools (1.0.0-previe...
David Pine
72

votes
1

answer
77.9k

Views

JSON serialization/deserialization in ASP.Net Core

Since there is no JavaScriptSerializer, what native implementation can be used to handle this? I noticed JsonResult and I can format data to Json with this, but how do I deserialize? Or maybe I am missing some dependencies in project.json?
Jakub Wisniewski
80

votes
4

answer
77.6k

Views

Send HTTP POST message in ASP.NET Core using HttpClient PostAsJsonAsync

I want to send dynamic object like new { x = 1, y = 2 }; as body of HTTP POST message. So I try to write var client = new HttpClient(); but I can't find method client.PostAsJsonAsync() So I tried to add Microsoft.AspNetCore.Http.Extensions package to project.json and add using Microsoft.AspNetCore...
Rem
74

votes
2

answer
76.9k

Views

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

I want the example controller below to return a status code 418 with no contents. Setting the status code is easy enough but then it seems like there is something that needs to be done to signal the end of the request. In MVC prior to ASP.NET Core or in WebForms that might be a call to Response.End(...
Ron C
61

votes
9

answer
76.5k

Views

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 'Web...
kimbaudi
127

votes
3

answer
75.6k

Views

RS256 vs HS256: What's the difference?

I'm using Auth0 to handle authentication in my web app. I'm using ASP.NET Core v1.0.0 and Angular 2 rc5 and I don't know much about authentication/security in general. In the Auth0 docs for ASP.NET Core Web Api, there are two choices for the JWT algorithm being RS256 and HS256. This may be a dumb qu...
Rico Kahler
71

votes
7

answer
68.1k

Views

How to enable CORS in ASP.net Core WebAPI

What I am trying to do I have a backend ASP.Net Core Web API hosted on an Azure Free Plan (http://andrewgodfroyportfolioapi.azurewebsites.net/swagger/) (Source Code: https://github.com/killerrin/Portfolio-Backend). I also have a Client Website which I want to make consume that API. The Client Appli...
killerrin
47

votes
6

answer
67.2k

Views

How to read values from the querystring with ASP.NET Core?

I'm building one RESTful API using ASP.NET Core MVC and I want to use querystring parameters to specify filtering and paging on a resource that returns a collection. In that case, I need to read the values passed in the querystring to filter and select the results to return. I've already found out...
user1620696
57

votes
7

answer
66.6k

Views

How get current user in asp.net core

I want to get current user for getting information of user such as email. But i cant do that in asp.net core.I'm so confused This is my code. HttpContext almost is null in constructor of controller. It's not good get user in each action.I Wanna get information of user once and set them into ViewDat...
Mehran Hafizi
137

votes
8

answer
65.3k

Views

How to setup Automapper in ASP.NET Core

I'm relatively new at .NET, and I decided to tackle .NET Core instead of learning the 'old ways'. I found a detailed article about setting up AutoMapper for .NET Core here, but is there a more simple walkthrough for a newbie?
theutz
55

votes
5

answer
63.7k

Views

How to read connection string in .NET Core?

I want to read just a connection string from a configuration file and for this add a file with the name 'appsettings.json' to my project and add this content on it: { 'ConnectionStrings': { 'DefaultConnection': 'Server=(localdb)\\mssqllocaldb;Database=aspnet- WebApplica71d622;Trusted_Connection=True...
motevalizadeh
96

votes
6

answer
62.2k

Views

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...
Mickael Caruso
89

votes
2

answer
62.1k

Views

MySql.Data.MySqlClient.MySqlException: “The host localhost does not support SSL connections.”

I use msyql.data 8.08 and .net core to connect to mysql5.7.18 but following exception is being thrown: MySql.Data.MySqlClient.MySqlException:“The host localhost does not support SSL connections.” How to deal with it?
Sea
141

votes
4

answer
61.4k

Views

Token Based Authentication in ASP.NET Core

I'm working with ASP.NET Core application. I'm trying to implement Token Based Authentication but can not figure out how to use new Security System for my case. I went through examples but they didn't help me much, they are using either cookie authentication or external authentication (GitHub, Micro...
Grant
128

votes
2

answer
60.9k

Views

Unexpected outcome of node.js vs ASP.NET Core performance test

I am doing a quick stress test on two (kinda) hello world projects written in node.js and asp.net-core. Both of them are running in production mode and without a logger attached to them. The result is astonishing! ASP.NET core is outperforming node.js app even after doing some extra work whereas the...
undefined
43

votes
6

answer
60.4k

Views

Getting absolute URLs using ASP.NET Core

In MVC 5, I had the following extension methods to generate absolute URLs, instead of relative ones: public static class UrlHelperExtensions { public static string AbsoluteAction( this UrlHelper url, string actionName, string controllerName, object routeValues = null) { string scheme = url.Request...
44

votes
2

answer
60k

Views

Custom Authentication in ASP.Net-Core

I am working on a web app that needs to integrate with an existing user database. I would still like to use the [Authorize] attributes, but I don't want to use the Identity framework. If I did want to use the Identity framework I would add something like this in the startup.cs file: services.AddIden...
rgvassar
75

votes
5

answer
58.3k

Views

How to specify the port an ASP.NET Core application is hosted on?

When using WebHostBuilder in a Main entry-point, how can I specify the port it binds to? By default it uses 5000. Note that this question is specific to the new ASP.NET Core API (currently in 1.0.0-RC2).
Drew Noakes
133

votes
13

answer
58k

Views

How to register multiple implementations of the same interface in Asp.Net Core?

I have services that are derived from same interface public interface IService { } public class ServiceA : IService { } public class ServiceB : IService { } public class ServiceC : IService { } Typically other IOC containers like Unity allow you to register concrete implementations by some Key that...
LP13
110

votes
6

answer
57.1k

Views

How do I get client IP address in ASP.NET CORE?

Can you please let me know how to get client IP address in ASP.NET when using MVC 6. Request.ServerVariables['REMOTE_ADDR'] does not work.
eadam
100

votes
6

answer
56.7k

Views

How to enable CORS in ASP.NET Core

I am trying to enable cross origin resources sharing on my ASP.NET Core Web API, but I am stuck. The EnableCors attribute accepts policyName of type string as parameter: // Summary: // Creates a new instance of the Microsoft.AspNetCore.Cors.Core.EnableCorsAttribute. // // Parameters: // poli...
Oluwafemi
84

votes
8

answer
55.9k

Views

How to use npm with ASP.NET Core

I'm using npm to manage the jQuery, Bootstrap, Font Awesome and similar client libraries I need for my ASP.NET Core application. The approach that worked for me started by adding a package.json file to the project, that looks like this: { 'version': '1.0.0', 'name': 'myapp', 'private': true, 'devDep...
Carlos Figueroa