Ask your technical questions on forums or here :
ASP.NET or MVC | C# | Windows Phone
Microsoft Technology Journals by Abhimanyu K Vatsa
HOME ABOUT RAZOR BOOK SPEAKING MVC ASP.NET JQUERY VIDEOS EBOOK ARCHIVE
Showing posts with label Tips-Trick. Show all posts
Showing posts with label Tips-Trick. Show all posts

17 Jun 2016

Caching in MVC with Donut Caching - excluding caching login

The main purpose of using caching is to dramatically improve the performance of your application. This is nothing but output caching that means whatever you see is cached, and exact similar things is display to everyone.

I recommend you to read Output Caching in MVC post before you read here, because you should be very careful when using any caching mechanism. Or, if you already know output caching, keep reading here only.

Biggest problem you face

If you display user login status on page which you want to cache, then you need to be careful. With output cache attribute [OutputCache(....)] caches everything on page and it will not exclude caching of some portion like login status.

In the situation, the best caching library you should use is Donut Caching (aka Donut Output Caching). Let’s understand its uses.

Using Donut Caching

The best way to add donut caching to your MVC project is to use the NuGet package. From within Visual Studio, select Tools | Library Package Manager and then choose either Package Manager Console or Manage NuGet Packages. Via the console, just type install-package MvcDonutCaching and hit return. From the GUI, just search for MvcDonutCaching and click the install button.

Excluding from being cached

The package adds several overloads to the built-in Html.Action HTML helper. The extra parameter in each overload is named excludeFromParentCache. Set this to true for any action that should not be cached, or should have a different cache duration from the rest of the page.

@Html.Action("Login", "Account", true)

Here Login is a method inside Account controller, you should define this method like:

public class AccountController : Controller
{
    [ChildActionOnly]
    public ActionResult Login()
    {
        return PartialView("_LoginPartial");
    }
}

Cache rest of the page

The package also include a DonutOutputCacheAttribute to be used in place of the built-in OutputCacheAttribute. This attribute is typically placed on every controller action that needs be be cached.

You can either specify a fixed duration:

[DonutOutputCache(Duration = "300")]
public ActionResult Index()
{
    return View();
}

Or, use a cache profile:

[DonutOutputCache(CacheProfile = "TenMins")]
public ActionResult Index()
{
    return View();
}

If you are using cache profiles, be sure to configure the profiles in the web.config. Add the following within the system.web element:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="TenMins" duration="400" varyByParam="*" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

You can also configure the output cache to use a custom provider:

<caching>
  <outputCache defaultProvider="DistributedCacheProvider">
    <providers>
      <add name="DistributedCacheProvider" type="DevTrends.Example.DistributedCacheProvider" />
    </providers>
  </outputCache>
</caching>

Note, that a custom provider is not detailed here but you can write one fairly easily by subclassing System.Web.Caching.OutputCacheProvider. A number of implementations are also available on the web.

Hope this helps.

26 Apr 2016

Credential parameter can only be used with Organization ID credentials - Azure Deployment or Continuous Deployment Error

I faced this error while Azure Web App Deployment starts in after Continuous Integration. Here's the detailed information you see in the error:

-Credential parameter can only be used with Organization ID credentials. For more information, please refer to http://go.microsoft.com/fwlink/?linkid=331007&clcid=0x409 for more information about the difference between an organizational account and a Microsoft account. There was an error with the Azure credentials used for deployment.

 Error Screenshot:


If you face this issue, here's quick way:

Step 1

Edit existing build definition and select "Azure Web App Deployment" and then click on "Manage".


Now you will be redirect on new tab.

Step 2

On new tab click on "Update service configuration" and this will open a dialog box where you need to select "Certificate Based" option and then fill up the details. You can click on "Publish settings file" to download, this file will have everything you need to fill here.


Now click on "ok" button to save changes and then fire another build and this will work now.

Hope this helps.

10 Mar 2016

Moving SQL Azure to local SQL or Azure SQL to VM SQL

There are many requirements like moving database between:

1. SQL Azure to local development SQL Server
2. SQL Azure to Virtual Machine SQL Server
3. Development SQL Server to Production SQL Server
4. Production SQL Server to Development SQL Server

Rename database data files and logical files in SQL Server

When I rename database using SQL Server Management Studio I always change its data files (mdf, ldf) name as well as logical file name of the resource. And this is very common need for SQL Server guy.

30 Nov 2015

How to remove/update default header and footer from PayPal sandbox page

I was updating an old website that uses PayPal as one of the payment gateway option. When I was testing the website I realized default header and footer texts.

20 Nov 2015

Search in selected file types Visual Studio

This is a very important feature which is worth useful when making necessary changes that may match in multiples file types but you are only supposed to change in selective files types.

10 May 2015

Export table data into excel file

Exporting table data (DOM data) on the HTML page to excel file is a very common requirement. The simplest approach or say the completely client side approach to achieve this is to export the DOM data using client side scripts.

3 May 2015

Remove Header & Footer from Print Preview in Google Chrome

Recently I had a requirement when one printing the web page should not display header and footer, basically they are automatically inserted by the browsers. Here is what this looks like:

18 Feb 2014

What is LESS (style-sheet language)? Exploring LESS coding features inside Visual Studio - Web Essential?

In this post we are going to talk about LESS and its introduction, coding, compilation and features available in Visual Studio Editor with Web Essential extension. Before talking about anything let’s understand what is LESS and how it can help developers.

12 Feb 2014

Creating Image Sprite in Visual Studio - Web Essential

In this article you will learn how to create image sprite using Visual Studio Web Essential Extension. Suppose you have a website that uses lots of images and each image loads individually which increases number of requests and makes website slow. We need quick website optimization to fix it.

9 Feb 2014

Paste Image from clipboard onto Visual Studio Editor : Video - Web Essential

In this article you will learn how to paste clipboard image onto Visual Studio Editor. Like we can paste clipboard image in MS-Word, it is possible to paste in Visual Studio Editor with the help of Web Essential Visual Studio Extension.

8 Feb 2014

Image Optimization in Website - Web Essential

In this article you will learn about Image Optimization using Web Essential Visual Studio extension. You will learn how we can optimize individual image by right clicking on image file, how to optimize group of images in the folder, how to optimize image in CSS file and how to optimize Base64 Encode Image (dataUri) in CSS file.

Base64 Encode Image in Website - Web Essential

In this article you will learn about Base64 Encode Image, why we encode image and how it can improve website performance. We are going to make use of Web Essential which is a must have Visual Studio extension.

3 Mar 2013

Logging Errors with ELMAH (Error Logging Modules And Handlers) in MVC 4

ELMAH is an alternative to HMS (Health Monitoring System) for logging errors which is free and available under open-source which is created and managed by Atif Aziz. HMS was (was...why?) great (with few pains) because it has option to capture all events as well as errors. With HMS developers has to design the UI for viewing the log information but with ELMAH, you just need to hit a URL to view log information.

2 Mar 2013

Removing or Customizing View Engines in MVC

In this article you will learn how to remove or customize View Engines which is not being used by application.

22 May 2012

Auto-Implemented Properties for trivial Get (getter) and Set (setter) in C#

In the example given below, you will look at a very good example on auto-implemented properties for trivial get (getter) and set (setter).


There is a shortcut way to create property with its definition just by pressing keyboard keys.


Find here Tips: shortcut way to create properties in C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Automatically_Implemented_Properties
{
    class Program
    {
        static void Main(string[] args)
        {
            // Intialize a new object.
            Customer cust1 = new Customer(500.50, "Abhimanyu", 12);
            //Modify a property
            cust1.TotalPurchases += 0.50;
            Console.WriteLine("Total Purchase = " + cust1.TotalPurchases);
            Console.WriteLine("Name = " + cust1.Name);
            Console.WriteLine("Customer ID = " + cust1.CustomerID);
            Console.ReadKey();
        }
    }
 
    //Customer Class
    class Customer
    {
        // Auto-Impl Properties for trivial get and set
        public double TotalPurchases { get; set; }
        public string Name { get; set; }
        public int CustomerID { get; set; }
 
        // Constructor
        public Customer(double purchases, string name, int ID)
        {
            TotalPurchases = purchases;
            Name = name;
            CustomerID = ID;
        }
    }
}

There are couples of key points to note when using automatic properties. The first is that we don't define the bodies of the getter and setter. The second is that we don't define the field that the property is backed by. Both of these are done for us by the C# compiler when we build our class. Using an automatic property is no different from using a regular property as code given above. By using automatic properties, we save ourselves some typing, create code that is easier to read, and still preserve the flexibility that a property provides.

A Nice YouTube Post for the reference



Tips: Commenting and Uncommenting codes

In this video tutorial you are going to learn how to comment and uncomment your codes in a quick way.



Tips: shortcut way to create properties in c-sharp

You could type "prop" and then press tab, that will generate the following.

        public TYPE Type { get; set; }

Then you change "TYPE" and "Type"

        public string myString {get; set;}

You can also get the full property typing "propfull" and then tab, that would generate the field and the full property.

        private int myVar;
        public int MyProperty        {
   
        get { return myVar;}
   
        set { myVar = value;}        }