Skip to main content

Posts

Consuming Services in ASP.NET Core MVC Controller

Another interesting feature of ASP.NET Core is service consumption using Dependency Injection. I already provided the overview of Dependency Injection in my earlier articles. Moving further, in this article we will see how one can inject and consume services to controller using dependency injection. In ASP.NET, you can access services at any stage of your HTTP pipeline through dependency injection. By default, it provides Constructor injection, but it can easily be replaced by any other container of your choice. Before moving ahead, one point is very important to understand and that is lifetime of services. ASP.NET Core allows you to configure four different lifetimes for your services. Transient - Created on every request Scoped - Created once per request Singleton - Created first time when they are requested Instance - Created in ConfigureServices method and behaves like a Singleton To understand the service consumption in easier way, we will create a sample applica

Tag Helpers in ASP.NET Core 1.0

Exclusive Amazon Deals This article is for those who would like to use Tag Helpers over old Razor/HTML helpers. Tag Helpers, another new feature of ASP.NET Core. Let’s quickly have a look at it. What are Tag Helpers? As per ASP.NET documentation:                     " Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files ". Tag Helpers are somewhat similar to HTML Helpers/Razor Helpers introduced in previous version of ASP.NET MVC but those helpers were not that easy to understand for web designers because of inline C# method calls. As web designers are more inclined towards HTML, a simpler and comfortable approach was required. Fortunately, ASP.NET Core 1.0 provided that. Now one need not to use HTML Helpers to build cshtml forms. Means when you had to write this: Now you can write this: As you can see a bove format is very easy to understand as it is purely a HTML syntax. How Tag Helpers wor

Handling 404 Error for Unknown File Extensions in ASP.NET Core 1.0

In my previous article, I already briefed about Static Files in ASP.NET Core. Now let’s move bit further and know about how ASP.NET treats content files which are of unknown type for any browser. You might be aware that ASP.NET Core middleware serves only those content files which falls under known content types. As of now, it defines close to 400 known file content types. Now question is, how browser will respond to unknown content type files? Let’s understand it better with an example. I’m having an image file named SampleData with extension as .shweta. Here shweta is my custom extension and browser has no information about it. Browser has no idea about how to render this type of file. Please note, this is a static resource placed on wwwroot and has to be served to client. Now quickly open SampleData.shweta in browser and see what happens?           Uhh! This is HTTP 404 Not Found L But is this message correct? If you will go back and cross check your solution

Fixing ‘DNX design time process error in VS 2015’

Exclusive Amazon Deals Today I thought to create my first demo using Visual Studio 2015 for ASP.NET Core 1.0. So, I opened VS2015 and chose Web Application template under ASP.NET 5 Templates. Visual Studio started creating a project for me. Although my project was created but at the same time some DNX error was thrown as shown below:                     I tried various options including restarting machine, re-installing Visual Studio, etc. But Alas ! Nothing worked. But on surfing for few hours, I found a solution which worked for me. You can run any of the below commands: dnvm update-self OR dnvm upgrade   Open Visual Studio 2015 and do dnvm update using command line as:     Once this command was executed, I didn’t get that error again. Hope reading this blog will save your few minutes of troubleshooting. Enjoy learning.

Features of ASP.NET Core 1.0

Exclusive Amazon Deals ASP.NET Core 1.0 is a new open-source framework for building modern Web applications. As compared to previous versions of ASP.NET there are many major changes happened. I’ll try to capture most of the changes in this blog post. ASP.NET Core 1.0 was initially named as ASP.NET 5. I’m dedicating this blog post to showcase the major features of ASP.NET Core 1.0. Single aligned web stack ASP.NET Core 1.0 is a single aligned web stack on top of ASP.NET for Web API and Web UI as shown in below figure (image taken from mva):   Above figure clearly depicts that there is very less sharing between these three stacks. If you will see from ASP.NET template view, you will notice that all the three options are dimmed out as shown below: Much leaner framework with reduced surface area ASP.NET Core 1.0 is no longer based on System.Web.dll. As per dzone , typical HTTPContext object graph takes 30K in memory while new implementation takes around 2K.I