Category: ASP.NET

ASP.NET Core 1.0

In this opportunity I will be writing about one of the most used (and newest) and functional server-side web application framework: ASP.NET Core. Before entering into the ecosystem of this tool allow me to give you some feedback that may be needed in order to have a better understanding of topics related with versions, features and releases (this one is quite new). core

What is ASP.NET Core 1.0?

ASP.NET Core 1.0 is, in short terms, the evolution of ASP.NET 4.6. If you are thinking that it is just a superior version of ASP.NET 4.6 take away that myth of your head because ASP.NET Core 1.0 is an entire new based-cloud applications framework written from scratch, with a new focus different from its predecessor.
Two of the most appreciated characteristics of this new framework are:
First it is open source (supported by Microsoft) meaning that you can find and download the code on GitHub , also contribute to the development of it, and second, it is cross-platform, so you can run it either in Windows, Linux or Mac OS.

Advantages and Disadvantages. Looking back to ASP.NET 5.


- Open source: as I mentioned before, this is a huge advantage and everybody loves it.
- Cross-platform: this is an obvious point, run it everywhere!.
- Modular design: the main stuff here is NuGet packages. Developers can use NuGet Package Manager to install libraries to extend the features of apps so now is easier to maintain the code due to the benefits of package-based software.
- MVC and WEB.API are together in one: Back in ASP.NET, MVC and WEB.API were based on different versions of the framework and because of this some differences related to namespace and classes were present and tend to confuse sometimes. Now arrives MVC 6, a unique collection of objects with a unique namespace (Microsoft.AspNet.Mvc), this standard simplifies the development process.
- Dependency injection: Before this was available as an ad-on but now it is built-in the framework, but still third party’s solutions like Autofac should be functional.
- Productivity: The keyword here is Kestrel, a lightweight web server incorporated by Microsoft as an alternative to developer’s who does not want to use heavy options to host development. With Kestrel you can run the site in OSX.
- Easier integration with client-side frameworks like AngularJS, KnockoutJS and Bootstrap.


- Issues with documentation in certain way: When in May of 2016 the RC2 of ASP.NET Core 1.0 was released the documentation was 'incompleted' because a lot of topics were without text. Today this situation was patched but sometimes you can find some cases, take in count that ASP.NET Core 1.1 is out too. God bless StackOverflow! 😉
- Sometimes when you are searching for information maybe a little bit hard, because this framework had many names in the past, first vNext, next ASP.NET 5 and today ASP.NET Core, this is not helpful.
- Compatibles solutions between versions not always is the best. You can find a solution to a specific trouble but it may not work because maybe it was written to a previous version. Thanks to GitHub and open source this can be fixed eventually because there is always an alternative.

What about ASP.NET Core 1.1?

The .NET Core 1.0 was updated with some changes, you can see them all here in the release notes.

Some of them are:
- Improved middleware in all sense through the user interface.
- Improved deployment of ASP.NET core apps on Microsoft Azure, increased the cloud support.
- Now other hosting options are available, beside IIS.

Let’s see an example of code, in this case it going to be an URL Rewriting Middle ware: (this was taken from here, check it if you want to see more examples.)

“URL Rewriting allows mapping a public URL space, designed for consumption of your clients, to whatever representation the downstream components of your middleware pipeline require as well as redirecting clients to different URLs based on a pattern.”

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
namespace MyApplication {

public class Startup {

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 

var options = new RewriteOptions()
.AddRedirect("(.*)/$", "$1") // Redirect using a regular expression
.AddRewrite(@"app/(\d+)", "app?id=$1", skipRemainingRules: false) // Rewrite based on a Regular expression
.AddRedirectToHttps(302, 5001) // Redirect to a different port and use HTTPS
.AddIISUrlRewrite(env.ContentRootFileProvider, "UrlRewrite.xml") // Use IIS UrlRewriter rules to configure
.AddApacheModRewrite(env.ContentRootFileProvider, "Rewrite.txt"); // Use Apache mod_rewrite rules to configure

// More Code



From .Net Framework 4.6 to .Net Core 1.0 : core chart

This images illustrates the main changes with its predecessor. I recommend you to read this post to understand some considerations before migrating from .NET 4.6 to .NET Core 1.0 as well as strategies and advices from the author to achieve it. In case you are not sure about what solution suits you, and need to pick, this post can give you a clear understanding.

Other resources to learn more about ASP.NET Core


Microsoft Virtual Academy – Introduction to ASP.NET Core

Building your first ASP.NET Core MVC application

Building Your First Web API with ASP.NET Core MVC and Visual Studio


How to avoid js files cache script bundle with razor

It happened to me, that I was working on an ASP.NET MVC Razor with AngularJs application, and every time that the team make a change in the AngularJs files and deploy to Production, the users start complaining about unresolved issues in the application. Those issues were because of the browser cache the JavaScript files in the client side. So in this article, I'll try to find an optimal solution to this problem, meaning that the javascript bundle should avoid cache when a new version of the application is deployed to production.
bundle cache

Avoiding browser cache:

We have to consider that one of the most popular techniques to avoid browsers cache with javascript files is to add a variable at the end of the file name, like this: src="/app/app.js?nocache=12365434576".

  <script type="text/javascript" src="/app/app.js?nocache=12365434576"></script>

Now this resolve the issue partially since 12365434576 it's going to be a random number that always going to avoid the cache, and we don't want that, we want to avoid cache only when a new version is deployed, for a better performance. So what we need to do is, instead of set the nocache var to a random number we are going to user the version of the application, like this:

Bundle Code:

We had a code like this to compose the bundle in App_Start/BoundleConfig.cs

 private static void AddAppBundles(BundleCollection bundles)
            var path = "admin";
            var scriptBundle = new ScriptBundle("~/js/app");
            var FullPath = HttpContext.Current.Server.MapPath(string.Format("~/{0}", path));
            if (Directory.Exists(FullPath))
                    // Order matters
                    string.Format("~/{0}/app.module.js", path),
                    string.Format("~/{0}/app.core.module.js", path)

                    .IncludeDirectory(string.Format("~/{0}", path), "*.module.js", true)
                    .IncludeDirectory(string.Format("~/{0}", path), "*.js", true);

With the code above we can include all the javascript files found in "path" variable in our _Layout.cshtml page, with this code:


The code is going to render something like this:

  <script type="text/javascript" src="/app/app.js"></script>
<script type="text/javascript" src="/app/config.js"></script>
<script type="text/javascript" src="/app/dashboard/dashboard.module.js"></script>
<script type="text/javascript" src="/app/layout/layout.module.js"></script>
<script type="text/javascript" src="/app/blocks/apiendpoint.config.js"></script>
<script type="text/javascript" src="/app/blocks/apiendpoint.provider.js"></script>

The question here is how we can render something like this src="/app/app.js?nocache=1.28.16145.10", when we render the bundle in razor.
We are going to use @Scripts.RenderFormat.


    string version = typeof(yourProjectNamespace.WebApiApplication).Assembly.GetName().Version.ToString();
    <!--app scripts.-->
    @Scripts.RenderFormat("<script type=\"text/javascript\" src=\"{0}?nocache="+ version +"\"></script>", "~/js/app")

This is an example of the result rendered:

  <script type="text/javascript" src="/app/app.js?nocache=1.28.16145.10"></script>
<script type="text/javascript" src="/app/config.js?nocache=1.28.16145.10"></script>
<script type="text/javascript" src="/app/dashboard/dashboard.module.js?nocache=1.28.16145.10"></script>
<script type="text/javascript" src="/app/layout/layout.module.js?nocache=1.28.16145.10"></script>
<script type="text/javascript" src="/app/blocks/apiendpoint.config.js?nocache=1.28.16145.10"></script>
<script type="text/javascript" src="/app/blocks/apiendpoint.provider.js?nocache=1.28.16145.10"></script>

Hope this was helpful. 🙂

State Management in ASP.Net

In this article I'll try to cover the different techniques session state management in ASP.Net applications. With this we are going to be able to determine the best approach for our solution when we manage the session states in ASP.Net applications.
session state management

These are the session state management options in

  • Off
  • StateServer
  • InProc
  • SQLServer
  • Cookies
  • Query String
  • Custom

Fot Off|StateServer|InProc|SQLServer, we will need to specify the session state tag in the web.config. This is a template code:

<sessionState mode="Off|StateServer|InProc|SQLServer"
              timeout="number of minutes"
              sqlConnectionString="sql connection string"
              stateNetworkTimeout="number of seconds"/>

Off Mode

Off mode, which disables session state.

StateServer Session State mode

StateServer Session State mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. StateServer session runs as a Windows service and would help to minimize database traffic.

    <sessionState mode="StateServer"

InProc Session State

InProc Session State, this mode stores session state with the ASP.NET worker process (in memory on the Web server). It is not valid in a web farm configuration.

      <sessionState mode="InProc"

SqlServer Session State

SqlServer Session State. stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. Although, It does support a web farm configuration, you should not use this if you want to minimize database traffic.

    <sessionState mode="SQLServer"
      sqlConnectionString="Integrated Security=SSPI;data 
        source=SampleSqlServer;" />

Cookie State mode

Cookie State mode. Cookies are stored on the client, so the application would not be able to keep the state if they switched to a different device or a different browser. Also the user needs to have the cookies enabled in their browser

Query String

Query String It can be used for passing limited state information across request boundaries, this solution would not be able to keep the state if they switched to a different device. This solution will work even if the user have disabled the cookies in the browser.

Custom mode

Custom mode, which enables you to specify a custom storage provider.

    <add name="OdbcSessionServices" 
      connectionString="DSN=SessionState;" />

        <add name="OdbcSessionProvider"
          writeExceptionsToEventLog="false" />

These are the options we have in to manage the session state in an application. Hope this was helpful.