How to create Html Helper in MVC using Extension Method ?



There are 2 option to create the Htmlheplers in MVC i.e

1. using C# Extension Method
2. Using Razor declaration @Helper Sysntax

Here we will create very simple htmlhelper i.e Truncate. Whichone can be used for truncating the string.

Step1: Create the simple blank Mvc application, like this


Step 2: Add the Blank Controller i.e HomeController and Pass some long string Value from Controller to View i.e Index.


Step 3: Create C# HtmlHelpers class in Seperate Helper folder and Write the Extension Method to truncate the string like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RazorHelper.Helper
    public static class HtmlHelpers

        public static string Truncate(this HtmlHelper helper, string input, int length)
            if (input.Length <= length)
                return input;
                return input.Substring(0, length) + ".....";



Step 4: Build the application and Call the Helper Method in Index view like this


Note: Donot forget to call the namespace otherwise you canot call the extension Method.

Step 5: Run the application


Here we show how to implement Extension Method of C# in real application and How create the Html Helper on basis of our requirement.


How to change the Default Company Name while creating window Setup Installer ?

If you are creating the Project Setup installer in Window Application then Author and manufacturer Name will come like “Default Company Name”. But if you have to change name then you can do like this


Step 1: Go to project Setup and Click F4 like this Image



Step 2: Change the name in Deployment Project Properties like this Image



Step 3: Build the application.

I hope this will help somebody who is new to window application.


How to use log4net in Project ?

Recently i had used log4net error handling plugin in one of my recent project. Previously i was using enterprise library for this task. But this is very flexible and essay to use in application.
We can use Log4net in our application using this few steps

Step 1: Install the Log4net plugin like this


Step2: Inside the Configration section of Webconfig, keep this code related with log4net like this

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"></section>

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="C:\MyLog\test.txt"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
      <level value="All" />
      <appender-ref ref="LogFileAppender" />

Step 3: In global.aspx file, keep the code like this

protected void Application_Start(object sender, EventArgs e)

Step 4: Now implement the error handling in your code like this

protected void btnClick_Click(object sender, EventArgs e)
log4net.ILog logger = log4net.LogManager.GetLogger(typeof(_Default));
// This is used for generating the error in code.
int a;
a = Convert.ToInt32(txtNum.Text);
catch (Exception ex)


Now if there will be any exception occur in application then it will create the log in C:\MyLog\test.txt