Skip to main content

Posts

Reading version information from project.json

As of now, many of you are aware about the information stored in project.json file in ASP.NET Core 1.0. My this blog post will tell, how to retrieve the version number from project.json file. Below is the screenshot of my json file: And below is the code to get the version number: public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // Reading project.json file var projConfig = new ConfigurationBuilder ().AddJsonFile( "project.json" ).Build(); //app version number var appVersion = projConfig[ "version" ]; // SQL Server version number var sqlServerVersion = projConfig[ "dependencies:EntityFramework.MicrosoftSqlServer" ]; } Hope this post was helpful.

Application State in ASP.NET Core 1.0

Introduction to Application State Application state provides a way to store in-memory data, which are smaller in size. It includes both global and user-specific data. Such data can be used across application and can be used by all users. Prior to ASP.NET Core 1.0 also there were Application and Session state options available to store such sort of data. Ways of managing Application State? Now question is, which state storage provider is to be used and when? It is influenced by variety of factors: Size of data Format of data Duration to persist data Sensitivity of data, etc. Based on your answers, Application State can be managed in variety of ways like: HTTPContext Cookies Session Querystring and Post Cache Other options (EF, Azure Table Storage, etc.) As part of ASP.NET Core 1.0 release, there is change in HTTPContext object. Hence I’ll emphasis on that. HTTPContext: Items collection of HTTPContext is used to store data which is required...

Consuming Services in ASP.NET Core MVC View

In continuation to my previous post entitled 'Consuming Services in ASP.NET Core Controller' on injecting services in controller, this time I'm writing on how to inject services directly in MVC View. In order to achieve this, a new keyword @inject is used. Here I'm not writing entire code again as it can be referred from my previous article. Let's register the service in ConfigureServices method as: public void ConfigureServices( IServiceCollection services) { ... ... services.AddTransient<IGUIDService,GUIDService>(); } Next is to inject service inside a View as: @inject IGUIDService guidService Now service is injected and available for use. Let's quickly use it: @ using CustomTagHelper.Services; @ inject IGUIDService guidService <p>@guidService.GenerateGUID()</p> Run your application and you will be able to see the required output as: Hope you enjoyed learnin...

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...

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 bov...

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 cr...

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.