C# (Net Core) – Display Friendly Name for Enumerator

To display a friendly name for enumerator element, we can use the Description annotation (we need to include the System.ComponentModel  namespace): using System.ComponentModel; public enum MyEnum { [Description("Success")] Success = 0, [Description("Error")] Error = 1 } Now we can use the normal  ToString()  to obtain the friendly name of the enumerator: MyEnum.Success.Success.ToString()

Entity Framework Core: set connection string on ASP Net Core

If we had a connection string stored into the appsettings.json file and we want to set our DbContext class to use this connection string, we need to call our DBContext constructor with che connection string parameter into the ConfigureService method of the class StartUp.cs. For example, into the appsettings.json file we declare the connection string like the … Continue reading Entity Framework Core: set connection string on ASP Net Core

C#: how to validate XML with a set of XSD schemas

When we are using an XSD schema to validate an XML document, if this schema has other nested XSD schemas, if we have a myCustomType declared into the nested XSD and we don't use XmlSchemaSet to validating the XML, we can get the error: Type 'myCustomType' is not declared For example, if we have the myCustomType defined … Continue reading C#: how to validate XML with a set of XSD schemas

Asp Net Core 2 – Angular: download file from Controller

If we have an Angular client app that needs to download a file (for example a PDF file) from an Asp Net Core 2 api Controller , we can follow what is described in this post. The first implementation is on the server side (an Asp Net Core 2 Controller). So, for example into our controller UserController, … Continue reading Asp Net Core 2 – Angular: download file from Controller

C#: get anonimous properties length

To get the number of the properties contained by an objects collection (even Anonymous) like the following: var data = new [] { new { Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" }, new { Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" }, new { Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" }, new { Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" }, new { Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" }, new { Name="Brij", … Continue reading C#: get anonimous properties length

Asp Net Core 2.0: resolve error CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory

This error is caused by a memory leak problem of node.js. On a Visual Studio 2017 Asp Net Core 2.0 project started from the Angular CLI Template, that use WebPack to manage the build process, after some publish builds, due to file size increase, we can get this error: node node_modules/webpack/bin/webpack.js --env.prod EXEC(0,0): Error : CALL_AND_RETRY_LAST Allocation … Continue reading Asp Net Core 2.0: resolve error CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory

Asp Net Core 2.0: how to handle a post (or put ) request that includes a dictionary in the body

In this article we will see how to set in a controller a method that manages a post request (it could also be a put request) that receives in the body a data structure represented by a Dictionary. Suppose we have a Dictionary (in simplified form) that contains string keys with int values: {"userId":46,"referenceId":15} With … Continue reading Asp Net Core 2.0: how to handle a post (or put ) request that includes a dictionary in the body

Implement JWT authentication on Asp Net Core 2.0 and Angular

This sample demonstrates how to authenticate web pages using JWT token in ASP.NET Core 2.0 and Angular. For user management, it refers to its own repository.   Asp Net Core First step is write the method that configure Jwt authentication: // Configure authentication with JWT (Json Web Token). public void ConfigureJwtAuthService(IServiceCollection services) { // Enable the … Continue reading Implement JWT authentication on Asp Net Core 2.0 and Angular

Asp Net Core 2.0 MVC: how to use a Controller of an external assembly

It is a good practice to keep projects separate according to their logic. A solution of a web project could be divided into: ClientProject ControllerProject DataAccessProject TestProject The client is the startup project. To make sure that it is possible to use the ControllerProject classes (so located in an external assembly), in the Startup.cs of … Continue reading Asp Net Core 2.0 MVC: how to use a Controller of an external assembly

Entity Framework Core 2.0: how to display SQL command of the Linq to SQL statements on Asp Net Core 2.0.

When you use Linq to SQL sometime is usefull view the SQL translated command that is executed to read the data from the database. To do this you can use the Asp Net Core 2.0 logger to log the SQL command in the output window. In your Startup.cs of the startup project: // This method … Continue reading Entity Framework Core 2.0: how to display SQL command of the Linq to SQL statements on Asp Net Core 2.0.

Visual Studio 2017: resolve error Unexpected token ‘/’ or Unexpected token ‘>’ in the css file

If you are using Visual Studio 2017 and in your Angular project use in the css component the '/deep/' or '>>>' selector, the css validator report the error "Error (CssLint) Unexpected token '/'" or "Error (CssLint) Unexpected token '>'". Instead of disabling css validation, you can use a style in child components by defining it … Continue reading Visual Studio 2017: resolve error Unexpected token ‘/’ or Unexpected token ‘>’ in the css file

Asp Net Core 2.0: how to test a controller method that return type IActionResult

If you had a controller method that return an IActionResult like the following. public async Task MyMethod(int someParamValue) {   try   { ... // Return a list of MyClassType objects   List myResult = ...   return Ok(myResult); } catch (Exception e) { return BadRequest(e); } } To test on a unit test, you need … Continue reading Asp Net Core 2.0: how to test a controller method that return type IActionResult

Entity Framework Net Core 2.0: execute stored procedures and mapping result into a list of custom objects

The support for stored procedure in EF Core is similar to the earlier versions of EF Code first. You need to create your DbContext class by inherting the DbContext class from EF. The stored procedures are executing using the DbContext. Starting from this article where I found a great way to map the result of a … Continue reading Entity Framework Net Core 2.0: execute stored procedures and mapping result into a list of custom objects