Reduce the json size in MVC 6

In here we removed all default value and null in our json output.

services.AddMvc()
        .Configure<MvcOptions>(options => {
          var jsonOutputFormatter = new JsonOutputFormatter();
          jsonOutputFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
          jsonOutputFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;

          options.OutputFormatters.RemoveTypesOf<JsonOutputFormatter>();
          options.OutputFormatters.Insert(0, jsonOutputFormatter);
          });
Advertisements

Typescript Auto Build

When you used VS 2012 it’s won’t auto compile the Typescript to plain Javascript.

Open your project .csproj and paste this below line just below

<Target Name="TypeScriptCompile" BeforeTargets="Build">
 <Message Text="Compiling TypeScript files" />
 <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\tsc&quot; -target ES5 @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
 </Target>

.Net Web Developer Should Know?

1. Remove the HTTP Headers

By removing the header “X-Powered-By” you make a save around 29bytes for each requests, another reason as well you doesn’t want exposing stack trace.

Server: Microsoft-IIS/8.5
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.0
X-Powered-By:ASP.NET

<httpRuntime targetFramework="4.5.1" enableVersionHeader="false" />

    <httpProtocol>
      <customHeaders>
        <clear />
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>

 

//In Global put this
MvcHandler.DisableMvcResponseHeader = true;

Continue reading

View Engines MVC

By Default MVC project enable both engines “Web Form” & “Razor”. In some case we doesn’t really care either we using .aspx, .ascx, .cshtml or .vbhtml it’s will just work by itself.

ViewEnginesByDefault

When you enable both of the Engines the performance will be affect, since it’s will figure out the Web Form Engines first then the Razor Engines.

If you exactly know you going to used only Razor Engines, it’s good sign to j^st enable one of the Engines, since it’s will bring performance improvement as well. to enabled Razor Engine simple in your “Global.asax.cs” and put this line of code”

ViewEngines.Engines.Clear(); //remove all view engines
ViewEngines.Engines.Add(new RazorViewEngine()); //We j^st enable the Razor Engine in here.