How to create Html Helper in Asp.net MVC using Extension Method ?


HtmlHelper

Hi,

There are 2 option to create the Htmlheplers in Asp.net 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

Empty

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

HomeController

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;
            }
            else
            {
                return input.Substring(0, length) + ".....";
            }

        }

    }
}

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

Methodcall

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

Step 5: Run the application

Summary

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

 

F4

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

 

AuthorName

Step 3: Build the application.

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

 

How to use log4net in asp.net 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

Log4net

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

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

  <log4net>
    <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"/>
      </layout>
    </appender>
    <root>
      <level value="All" />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>
  

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

protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}

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));
try
{
// This is used for generating the error in code.
int a;
a = Convert.ToInt32(txtNum.Text);
Response.Write(a);
}
catch (Exception ex)
{
logger.Error(ex.StackTrace);
}

}

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