Base Controller in ASP.NET MVC Application

using LogExpress.ExceptionLogger;
using System;
using System.Globalization;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc; 
using EnDecryptor;

namespace GPOOH
{
    public abstract class BaseController : Controller
    {
        protected string BaseUrl = "http://localhost:63716/";
        protected string ConnectionString = WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

        protected string[] InputDateFormatsAllowed = {
                                                       "d/M/yy", "d/M/yyyy",
                                                       "d/MM/yy", "d/MM/yyyy", 
                                                       "dd/M/yy", "dd/M/yyyy", 
                                                       "dd/MM/yy", "dd/MM/yyyy",

                                                       "d-M-yy", "d-M-yyyy",
                                                       "d-MM-yy", "d-MM-yyyy", 
                                                       "dd-M-yy", "dd-M-yyyy", 
                                                       "dd-MM-yy", "dd-MM-yyyy",

                                                       "d.M.yy", "d.M.yyyy",
                                                       "d.MM.yy", "d.MM.yyyy", 
                                                       "dd.M.yy", "dd.M.yyyy", 
                                                       "dd.MM.yy", "dd.MM.yyyy"

                                                    };

        protected CultureInfo myCulture = new CultureInfo("bn-BD-gp");

        /// <summary>
        /// Encryption
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string EnCrypt(string str)
        {
            string enccryptedText = Encryptor.Encrypt(str);
            return enccryptedText;
        }

        /// <summary>
        /// Decryption
        /// </summary>
        /// <param name="encryptedText"></param>
        /// <returns></returns>
        protected string DeCrypt(string encryptedText)
        {
            string deccryptedText = Decryptor.Decrypt(encryptedText);
            return deccryptedText;
        }


        protected void LogException(Exception exception)
        {
            var path = HttpContext.Server.MapPath("~/App_Data");
            ExceptionLog.Create(exception, path);
        }  
    }
}


//Usage

public class SearchController : BaseController
{
    public ActionResult Index()
    {
        try
        {
            using (SqlConnection connection=new SqlConnection(this.ConnectionString))
            {

            }
        }
        catch (Exception Ex)
        {
            this.LogException(Ex);
        }
        
        return View();
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s