Skip to main content

Posts

All About Appsettings.json in ASP.NET Core 2.0

As most of you are aware that in ASP.Net Core, we don't have anything called Web.Config, where we use to write our connection strings or application specific settings. Rather, here we have the file named appsettings.json to store similar sort of information. There are few very common use cases where we need to maintain multiple appsettings.json file in a single solution. For example: Multiple application settings per application - When we need to maintain different-different application specific settings based on the application environment. Say, one can have one type of application settings for Development, another type of application settings for Production, another one for Staging, and so on. Needless to mention, all the appsettings file will have different names. To implement inheritance – If there are some common settings between multiple application settings file, in that case developer can come up with a base application settings file and on top of that specific file

Application Initialization and Configuration in ASP.Net Versions

Most of us might have worked upon various versions of ASP.NET and few of you must be aware about the major changes happened in application initialization and configuration phase. In this article, I'll be outlining few of those major changes starting from ASP.NET MVC, ASP.NET Core 1.x and ASP.NET 2.x. In ASP.NET: In an era of ASP.NET (prior to ASP.NET Core releases), loading of the application was handled by IIS or say Inetmgr was the one who use to call web application's entry point wherein Global.asax.cs use to provide the Application_Start()method.Below is the sample code snippet take from Global.asax.cx file:        public class MvcApplication : System.Web.HttpApplication   {        protected void Application_Start()        {               AreaRegistration.RegisterAllAreas();               FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);               RouteConfig.RegisterRoutes(RouteTable.Routes);               BundleConfig.RegisterBundles(BundleTa

Tip on MVC URL Routing

Recently, one of my colleague asked me a question, in which his routing was not working as expected. Let me mention the exact URL he was giving: URL 1:  http://localhost:port/StudentEnquiries/StudentEnquiries/44" URL 2: http://localhost:port/StudentEnquiries/StudentEnquiries/?StudentID=44  And his code snippet was as below:            public class RouteConfig       {           public static void RegisterRoutes(RouteCollection routes)           {               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");               routes.MapRoute(                   name: "Default",                   url: "{controller}/{action}/{id}",                   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }               );           }       }      Where he claimed that he didn’t change anything in his Route.Config file and his Controller looks something like this:

Invoking web application from console application (.Net Core) via command prompt

In this article, I’ll be giving a walkthrough on how to create a console application and changing that into a web application. Or in other words, invoking a web application from a .Net core console application. And that too completely from command prompt. If you are a command prompt lover, you may love it. So, let’s gear up and proceed step-by-step. Verify .Net Core If you are creating a .Net Core application for the very first time, then it is good to verify whether it is installed on the system or not and this can be done by typing a simple command  dotnet --version  as shown below:   Create Console application Now we are sure that required setup is present on our machine, we can proceed and create our first Console application  using command dotnet new console as shown below:   On successful execution of above command, you will see that Program file and project file is created in your local directory and same can be verified by opening Program.

Verify database prior to data insertion via EF

Recently, one of my colleague got a requirement on inserting data into database using EF. His issue was, how to verify if the database schema is proper or say all the columns in the tables matches with his POCO entities. Hope few of you must have come across similar scenarios. Here is the quick solution for this. In such scenarios, developers can do the schema compatibility check prior to inserting any data into the columns to ensure that model class still holds good with database tables. bool isModelValid = yourContext. Database . CompatibleWithModel ( true ); In the above method, passing the correct boolean value will do the trick for us. If the parameter value passed is true, then the framework will throw an exception if the schema doesn’t matched with your model class. Isn’t it a useful tip? Happy troubleshooting!!!

Ways to add dependency packages in .NET core

This would be a very short article on how to add dependencies in .Net Core . Well, there are many ways to achieve this. One is via Visual Studio and another way is through command prompt. Let’s quickly have a look.  1) From Visual Studio:  This is the straight forward way for the ones who want to use user interface to add dependencies. Right click on your project/library and get it from Nuget gallery.  2) From Command Prompt:   If you are a command prompt fan, then there itself, you have 2 choices. A) Open command prompt. Navigate to your project directory and simply fire: C:\<Your project directory> dotnet add package Microsoft.AspNetCore   B) Alternative you can go and add the reference directly in .csproj file as shown below: and restore it from command prompt using very simple command: C:\<Your project directory> dotnet restore Happy learning!!!