X

Subscribe To Our Mailing List

P.S I will never spam you...100% GUARANTEED!

Thursday, December 25, 2014

How to find tables used in stored procedure in sql server

In this "How to find tables used in stored procedure in sql server"article i am going to discuss about some SQL queries which are useful in our real time projects. We might have done these things manually but now lets see how useful these queries turns out to be.

First SQL query what i wanted to discuss is -

How to find tables used in stored procedure in sql server

How to find all stored procedure names which uses the given table

Query -
SELECT DISTINCT 
 obj.Name
 ,com.Text
FROM sysobjects obj 
     INNER JOIN syscomments com ON obj.ID = com.ID
WHERE obj.Type = 'P' AND com.Text LIKE '%' + 'yourtablename' + '%'
ORDER BY obj.Name
Lets create a function use this query in function -
IF EXISTS (
    SELECT * FROM sysobjects WHERE id = object_id(N'FN_GetAllStoredProcsByTableName') 
    AND xtype IN (N'FN', N'IF', N'TF')
)
    DROP FUNCTION FN_GetAllStoredProcsByTableName
GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Abhijith
-- Create date: 12/25/14
-- Description: Find all stored procedure details which is using given table
-- =============================================
CREATE FUNCTION FN_GetAllStoredProcsByTableName 
( 
 @tableName VARCHAR(50)
)
RETURNS 
@SPDetails TABLE 
(
 Name VARCHAR(50)
 ,Text VARCHAR(5000)
) 
AS
BEGIN
 
 INSERT INTO @SPDetails
 (
  Name,
  Text
 )
 SELECT DISTINCT 
  obj.Name
  ,com.Text
 FROM sysobjects obj 
  INNER JOIN syscomments com on obj.ID = com.ID
        WHERE obj.Type = 'P' AND com.Text LIKE '%' + @tableName + '%'
        ORDER BY obj.Name
 
 RETURN;
END
As you can see in the above function, table name is passed as an parameter and this table name is searched in all the stored procedures (Type = 'P'). Now lets execute the above function and lets see how the results will look like.

Before executing this function i have created tables "Student" and "Class"(Here i am not giving the table structures as these are trivial)and created a stored procedures "SP_GetAllStudents" and "SP_GetAllClasses" for demonstration purpose, which returns all the students and class details respectively nothing fancy.
Stored procedure SP_GetAllStudents -
CREATE PROCEDURE [dbo].[SP_GetAllStudents]
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    SELECT 
 Classes.ClassID 
 ,Students.StudentID
 ,Students.StudentName
    FROM Classes
     INNER JOIN Students ON Classes.ClassID = Students.ClassID
END
Now lets execute the function -
SELECT * FROM dbo.FN_GetAllStoredProcsByTableName('students')

Above function returns below resultset -

How to find tables used in stored procedure in sql server

Second stored procedure is not filtered because in that stored procedure only "Class" table is used.
Now lets move onto the next SQL query -

How to find tables used in stored procedure in sql server

Query -
SELECT DISTINCT 
  obj2.Name
FROM sys.sysobjects obj
 INNER JOIN sys.sysdepends dep ON obj.ID = dep.ID
 INNER JOIN sys.sysobjects obj2 ON obj2.ID = dep.DepID
WHERE obj.Type = 'P' and obj.Name = 'SP_GetAllStudents'
Now lets create a new function as we created earlier -
IF EXISTS (
    SELECT * FROM sysobjects WHERE id = object_id(N'FN_GetAllTableNamesBySpName') 
    AND xtype IN (N'FN', N'IF', N'TF')
)
    DROP FUNCTION FN_GetAllTableNamesBySpName
GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Abhijith
-- Create date: 12/25/14
-- Description: Find all tables used in given SP name
-- =============================================
CREATE FUNCTION FN_GetAllTableNamesBySpName 
( 
 @SPName VARCHAR(50)
)
RETURNS 
@TableNames TABLE 
(
 Name VARCHAR(50)
) 
AS
BEGIN
 
 INSERT INTO @TableNames
 (
  Name
 )
 SELECT DISTINCT 
  obj2.Name
 FROM sys.sysobjects obj
  INNER JOIN sys.sysdepends dep ON obj.ID = dep.ID
  INNER JOIN sys.sysobjects obj2 ON obj2.ID = dep.DepID
 WHERE obj.Type = 'P' and obj.Name = @SPName
 
 RETURN;
END

As you can see above stored procedure name is passed as a parameter and table names are searched. This will give below result set once executed with
"SP_GetAllStudents" -


SELECT * FROM dbo.FN_GetAllTableNamesBySpName('SP_GetAllStudents')

How to find tables used in stored procedure in sql server

Hope this article is useful. Please comment your thoughts.


Tuesday, December 23, 2014

Skip and take in sql server 2008

As we all know in LINQ we have the functionality of Skip and Take on list of objects , where we can skip some rows of list of objects and take the required list of objects. So here in this - "skip and take in sql server 2008" article i will try to explain the same functionality in SQL Server.
Before that lets see how skip and take functions work in LINQ -
To explain this i will create a class called "Employee.cs" as below -
//Example for Skip and take in sql server 2008
public class Employee
{
 public int ID { get; set; }
 public string Name { get; set; }
}
Now lets create a method which will return the list of employees -

//Example for Skip and take in sql server 2008
public static List<Employee> GetEmployees()
{
 List<Employee> employees = new List<Employee>();

 employees.Add(new Employee() { ID = 1, Name = "Jack" });
 employees.Add(new Employee() { ID = 2, Name = "Aaron" });
 employees.Add(new Employee() { ID = 3, Name = "Peter" });
 employees.Add(new Employee() { ID = 4, Name = "David" });
 employees.Add(new Employee() { ID = 5, Name = "Abel" });
 employees.Add(new Employee() { ID = 6, Name = "Adam" });
 employees.Add(new Employee() { ID = 7, Name = "Brian" });

 return employees;
}
Now my intention would be to get 2 employees from the above list namely "Abel" and "Adam" and skip first 4 employees of the list.

//Example for Skip and take in sql server 2008
List selectedEmps = emps.Skip(4).Take(2).ToList();
Now lets get into SQL and check the same functionality -
For demonstration purpose i will create a table called "Employee" and insert the same list of employees into that table.


CREATE TABLE Employee
(
 ID INT IDENTITY(100,1),
 NAME VARCHAR(50)
)

INSERT INTO Employee
VALUES
   ('Jack')
   ,('Aaron')
   ,('Peter')
   ,('David')
   ,('Abel')
   ,('Adam')
   ,('Brian')
Now i will go ahead and create a table valued function to get the table with required number of records.


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION FN_TakeNRows
(
 @startRowNo INT = 1
 ,@endRowNo INT = 1
)
RETURNS 
@filteredRows TABLE 
(
 ID INT
 ,Name VARCHAR(50)
 ,UniqueID INT
)
AS
BEGIN
 
 INSERT INTO @filteredRows
 (
  ID,
  Name,
  UniqueID
 )
 SELECT TOP (@endRowNo) allRows.ID, allRows.Name, allRows.uniqueID
 FROM
 (
  SELECT 
   ID
   ,Name
   ,ROW_NUMBER() over(order by ID) as uniqueID
  FROM Employee
 ) allRows
 WHERE allRows.uniqueID > @startRowNo
 
 RETURN;
END
GO
As in LINQ query we will pass the values 4 and 2 as shown below to get 2 employee records. Execute statement of the function is as below -


SELECT * FROM FN_TakeNRows (4, 2)
So here in the above execute statement similar to LINQ query it skips 4 employee records and takes 2 employee records.
The result is below -
skip and take in sql server 2008

Hope this article is useful. Please comment your feedback.

Open Xml Sdk 2.0 for microsoft office

Introduction
In this article i am going to explain about the common requirement of exporting the data to excel. Most of the product in the market now will be having this functionality for their business needs and there are many SDKs available too to fulfill this requirement.
When i got this requirement for my project i initially thought to use -  Interop Objects  (using Microsoft.Office.Interop.Excel). The drawback of this is - Since it is dependent on Microsoft Office after deploying to the production it expects Microsoft Office to be installed in the deployed environment. Sometimes it is feasible but most of the time it is not. Installing Microsoft Office only for this requirement does not make any sense isn't it ?
Now its time to go to other option - "Open XML SDK", which solves the above problem and its open source too :-)  

Open Xml Sdk 2.0 for microsoft office
Little more on OpenXML
Open XML is an open and standardized format for Office files. The standards used are:
  • ECMA-376   
  • ISO/IEC 29500-1:2008   
The Open XML file formats use an open standard and are based on well-known technologies: ZIP and XML. Now lets move on to the code part -

OpenXML in Code
Lets create a sample MVC application to demonstrate this - 
  • Create New Project -> ASP.Net Web Application -> MVC  ( here MVC is not mandatory as this article focus on OpenXML, since i am comfortable in that i am using it)
  • In Home Controller, Index Action call ExportExcel() method like below - 
public ActionResult Index()
{
    ExportExcel();
    return View();
}
Now lets create a sample dataset with sample data to populate the excel file -
public DataTable GetDatatable()
{
 DataTable dt = new DataTable();

 dt.Columns.Add("FirstName", typeof(String));
 dt.Columns.Add("LastName", typeof(String));
 dt.Columns.Add("Address", typeof(String));

 DataRow dr1 = dt.NewRow();
 dr1["FirstName"] = "John";
 dr1["LastName"] = "Martin";
 dr1["Address"] = "CA";

 DataRow dr2 = dt.NewRow();
 dr2["FirstName"] = "Rick";
 dr2["LastName"] = "Fleming";
 dr2["Address"] = "OH";

 DataRow dr3 = dt.NewRow();
 dr3["FirstName"] = "David";
 dr3["LastName"] = "Langer";
 dr3["Address"] = "SA";

 dt.Rows.Add(dr1);
 dt.Rows.Add(dr2);
 dt.Rows.Add(dr3);

 return dt;
}
As you can see the first snippet of code, we have method called - ExportExcel() which is the parent method to export the excel. The definition of this method - 
public void ExportExcel()
{
 DataTable dt = GetDatatable();
 string strFileFullPath= "D:\\Test.xlsx";

 ExportExcelFile.CreateExcel(dt, strFileFullPath);
}
In the above method path of excel is passed, if excel file not found in that location then the new file will be created in that location or else existing file will be used.
So below is the code snippet of "CreateExcel" method and its child methods -
public static class ExportExcelFile
{

 public static bool CreateExcel(DataTable dt, string xlsxFilePath)
 {
  DataSet ds = new DataSet();
  ds.Tables.Add(dt);

  try
  {
   using (SpreadsheetDocument document = SpreadsheetDocument.Create(xlsxFilePath
                                               , SpreadsheetDocumentType.Workbook))
   {
    WriteToTExcelFile(ds, document);
   }
   return true;
  }
  catch (Exception ex)
  {
   return false;
  }

 }

 private static void WriteToTExcelFile(DataSet ds, SpreadsheetDocument spreadsheet)
 {
  spreadsheet.AddWorkbookPart();
  spreadsheet.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

  spreadsheet.WorkbookPart.Workbook.Append(new BookViews(new WorkbookView()));

  WorkbookStylesPart workbookStylesPart = spreadsheet.WorkbookPart.AddNewPart("rIdStyles");
  Stylesheet stylesheet = new Stylesheet();
  workbookStylesPart.Stylesheet = stylesheet;

  uint worksheetNumber = 1;
  foreach (DataTable dt in ds.Tables)
  {
   string workSheetID = "Sheet" + worksheetNumber.ToString();
   string worksheetName = dt.TableName;

   WorksheetPart newWorksheetPart = spreadsheet.WorkbookPart.AddNewPart();
   newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

   newWorksheetPart.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

   WriteDataTableToExcel(dt, newWorksheetPart);
   newWorksheetPart.Worksheet.Save();

   if (worksheetNumber == 1)
    spreadsheet.WorkbookPart.Workbook
                                               .AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheets());

   spreadsheet.WorkbookPart.Workbook.GetFirstChild()
                                               .AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
   {
    Id = spreadsheet.WorkbookPart.GetIdOfPart(newWorksheetPart),
    SheetId = (uint)worksheetNumber,
    Name = dt.TableName
   });

   worksheetNumber++;
  }

  spreadsheet.WorkbookPart.Workbook.Save();
 }


 private static void WriteDataTableToExcel(DataTable dt, WorksheetPart worksheetPart)
 {
  var worksheet = worksheetPart.Worksheet;
  var sheetData = worksheet.GetFirstChild();

  string cellValue = "";

  int numberOfColumns = dt.Columns.Count;
  bool[] IsNumericColumn = new bool[numberOfColumns];

  string[] excelColumnNames = new string[numberOfColumns];
  for (int n = 0; n < numberOfColumns; n++)
   excelColumnNames[n] = GetExcelColumnName(n);

  uint rowIndex = 1;

  var headerRow = new Row { RowIndex = rowIndex };  
  sheetData.Append(headerRow);

  for (int colInx = 0; colInx < numberOfColumns; colInx++)
  {
   DataColumn col = dt.Columns[colInx];
   AppendTextCell(excelColumnNames[colInx] + "1", col.ColumnName, headerRow);
   IsNumericColumn[colInx] = (col.DataType.FullName == "System.Decimal") || 
                                                         (col.DataType.FullName == "System.Int32");
  }

  double cellNumericValue = 0;
  foreach (DataRow dr in dt.Rows)
  {
   ++rowIndex;
   var newExcelRow = new Row { RowIndex = rowIndex };  
   sheetData.Append(newExcelRow);

   for (int colInx = 0; colInx < numberOfColumns; colInx++)
   {
    cellValue = dr.ItemArray[colInx].ToString();

    if (IsNumericColumn[colInx])
    {
     cellNumericValue = 0;
     if (double.TryParse(cellValue, out cellNumericValue))
     {
      cellValue = cellNumericValue.ToString();
      AppendNumericCell(excelColumnNames[colInx] + rowIndex.ToString()
                                                                      , cellValue, newExcelRow);
     }
    }
    else
    {
     AppendTextCell(excelColumnNames[colInx] + rowIndex.ToString(), cellValue
                                                                      , newExcelRow);
    }
   }
  }
 }

 private static void AppendTextCell(string cellReference, string cellStringValue, Row excelRow)
 {
  Cell cell = new Cell() { CellReference = cellReference, DataType = CellValues.String };
  CellValue cellValue = new CellValue();
  cellValue.Text = cellStringValue;
  cell.Append(cellValue);
  excelRow.Append(cell);
 }

 private static void AppendNumericCell(string cellReference, string cellStringValue, Row excelRow)
 {
  Cell cell = new Cell() { CellReference = cellReference};
  CellValue cellValue = new CellValue();
  cellValue.Text = cellStringValue;
  cell.Append(cellValue);
  excelRow.Append(cell);
 }

 private static string GetExcelColumnName(int columnIndex)
 {
  if (columnIndex < 26)
   return ((char)('A' + columnIndex)).ToString();

  char firstChar = (char)('A' + (columnIndex / 26) - 1);
  char secondChar = (char)('A' + (columnIndex % 26));

  return string.Format("{0}{1}", firstChar, secondChar);
 }
}
Below is the file generated after exporting the data -

Open Xml Sdk 2.0 for microsoft office

Hope this helps. Comment below in case of clarifications or doubts.

Wednesday, December 17, 2014

jquery validation - Client side validation using jquery in asp.net mvc

Introduction for "client side validation using jquery in asp.net mvc"

In most of the cases we are going to use Data Annotations for validation in MVC. Let's not forget the point of unobtrusive client-side validation which turned out to be a boon for the developers. In unobtrusive client-side validation, validation rules are defined using attributes added to the generated HTML elements. This article will focus on adding the validation rules from client side.

client side validation using jquery in asp.net mvc
Let's get started

Now let's dive into the code part as how we can add the rules for the controls. Before that i need to discuss when we can use this type of approach – Consider a scenario where you are re-using the page with hide and show the controls of the page, eg : if one client wants textbox and another client wants dropdownlist for the same field in a same page and if the validation rules are different for both these controls then adding the rules from client side would be a better approach.

For enabling client side validation, we required to include following scripts in our view or layout page in the following order.

<script src="~/Scripts/jquery-{version}.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

Now lets create a new project for this and lets add the controller, models, views and javascript for our project.

  1. Create a new MVC project 
  2. Add a new controller called "ProductController"
  3. Add a new model called "ProductModel" and add the properties - Name, Description and Amount
  4. Add a new view called "Index.html" for our product
  5. Add a new javascript file for validation "productvalidation.js"

Now we will just set the validation rules for all the properties in the ProductModel in productvalidation.js file. Script for adding the validation rules for our controls in a page -

$(document).ready(function () {

    var regexAlpabetsOnly = /^[a-zA-Z\- ']*$/;
    var regexNumbersOnly = /^\d+$/;
   
    $('#Name').rules("add", {
       required: true,
       minlength: 2,
       messages: {
         required: "Please enter name",
         minlength: "At least 2 characters are mandatory"
       }
   });

   $('#Description').rules("add", {
      required: true,
      regex: regexAlpabetsOnly,
      maxlength : 20,
      messages: {
        required: "Please enter description",
        regex: "Please enter alphabets in description field",
        maxlength: "Exceeded maximum length of description"
      }
  });

   $('#Amount').rules("add", {
      required: true,
      regex: regexNumbersOnly,
      messages: {
        required: "Please enter Amount",
        regex: "Please enter numbers in amount field"
      }
   });

 });

As you see above nothing fancy all are straight forward and we have following validations -

  • For "Name" property we are adding required field validation and minimum length validation.
  • For "Description" property we are adding required field validation and regex validation to allow only alphabets.
  • For "Amount" property we are adding required field validation and regex validation to allow only numbers.

All are fine but only concern here is error messages are hardcoded. So its always a better idea to get these texts from resource file instead of hardcoding.

Now lets add the resource file and move these texts into resource file. For brevity i have not given the lump steps here -

Once its added the next would be to get the resource file data to javascript and that again is easy. Better option to do this is serialize the resource file and pass the JSON object to javascript.

Lets do that now -

In Controller lets add a method which will return JSON object -

[HttpGet]
public JavaScriptResult GetResourceFileData()
{
 return ResourceSerialiser.GetResourceSerailizedData(Resource.ResourceManager);
} 

For Serializing the resource file data -
 
public static JavaScriptResult GetResourceSerailizedData(ResourceManager resourceManager)
  {
     string cacheName = string.Format
       ("ResourceJavaScripter.{0}", CultureInfo.CurrentCulture.Name);

     JavaScriptResult value = HttpRuntime.Cache.Get(cacheName) as JavaScriptResult;

     if (value == null)
     {
        JavaScriptResult javaScriptResult = CreateResourceJSON(resourceManager);
        HttpContext.Current.Cache.Insert(cacheName, javaScriptResult);
        return javaScriptResult;
     }

      return value;
  }

  static JavaScriptResult CreateResourceJSON(ResourceManager resourceManager)
  {
       ResourceSet defaultSet = resourceManager.GetResourceSet
             (CultureInfo.GetCultureInfo("en"), true, true);
       ResourceSet resourceSet = resourceManager.GetResourceSet
             (CultureInfo.CurrentCulture, true, true);

       var resourceBaseName = resourceManager.BaseName;
       var jsonObjectName = resourceBaseName.Substring(resourceBaseName.LastIndexOf(".") + 1);

       StringBuilder sb = new StringBuilder();
       sb.Append("[");
       sb.Append("{");

       foreach (DictionaryEntry dictionaryEntry in resourceSet)
            if (dictionaryEntry.Value is string)
           {
                string value = resourceSet.GetString
                    ((string)dictionaryEntry.Key) ?? (string)dictionaryEntry.Value;
                sb.AppendFormat("\"{0}\":\"{1}\"", dictionaryEntry.Key, Encode(value));
                sb.Append(",");
           }


        string script = sb.ToString();
        if (!string.IsNullOrEmpty(script)) 
             script = script.Remove(script.Length - 1);


        script += "}]";
   

        JavaScriptResult result = new JavaScriptResult { Script = script };
        return result;
    } 

    static string Encode(string val)
    {
         val = (val).Replace("\"", "\\\"").Replace('{', '[').Replace('}', ']');
         val = val.Trim();
         val = System.Text.RegularExpressions.Regex.Replace(val, @"\s", " ");
         return val;
    }

In javascript lets add a new AJAX call to get the JSON object from controller method -

$.ajax({
  cache: false,
  type: "GET",
  url: "Product/GlobalResourceFileData",
  async: false, 
  dataType: "json",
  success: function (resourceData) {
   resourceFilevalues = resourceData[0];
  }
 });

" resourceFilevalues " is global variable which is used to receive the JSON object in AJAX call given above.

Now the hardcoded values will be replaced by the resource file properties like below -

$('#Name').rules("add", {
  required: true,
  minlength: 2,
  messages: {
   required: resourceFilevalues.NameRequiredErrMessage,
   minlength: resourceFilevalues.NameMinLengthErrMessage
  }
 });

 $('#Description').rules("add", {
  required: true,
  regex: regexAlpabetsOnly,
  maxlength : 20,
  messages: {
   required: resourceFilevalues.DescriptionRequiredErrMessage,
   regex: resourceFilevalues.DescriptionRegexErrMessage,
   maxlength: resourceFilevalues.DescriptionMaxLengthErrMessage
  }
 });

 $('#Amount').rules("add", {
  required: true,
  regex: regexNumbersOnly,
  messages: {
   required: resourceFilevalues.AmountRequiredErrMessage,
   regex: resourceFilevalues.AmountRegexErrMessage
  }
 });

So hope this article is useful and i agree this is the longest post i have written :-)

Monday, December 1, 2014

Invalid range in character set javascript error

Invalid range in character set javascript error

I faced this issue when i was working on IE 8 and this issue has driven me crazy.Since this is not a straight forward issue to fix, i tried to narrow down the problem and finally came to know the regular expression what i used for validation is the culprit for this.This issue would not have come if you are running your application in the high end browsers.

The line which was causing the problem -
var SpecialCharRegex = /^[0-9a-zA-Z',-\s]*$/;
After doing lot of googling i found the issue and the issue is "-" in this regular expression.This error is due to the hyphen, which is mistakenly being used to represent a range and is not properly escaped. To fix this lets add backslash before "-" and change the regular expression like this -
var SpecialCharRegex = /^[0-9a-zA-Z',\-\s]*$/;
Simple logic is either put "-" at the beginning or end of the character class or use backslash to do a regex escape.
It works like a charm !!

MVC override controller methods

As you have seen in my post -  Overriding RazorViewEngine in MVC here in this post also i am trying to override the path of the view but this time by overriding controller rather than creating the custom view engine.

Now lets dive in to the code part -

Here i am going to reuse the controllers and folders created in the last post and I will be creating a new controller named "BaseController.cs" and this will be inherited from "Controller" class. This BaseController is used as a base class for all the controllers like Home,Contact etc.

public class BaseController : Controller
{
     protected override ViewResult View(string ViewName, string masterName, object model)
     {
         ViewResult renderview = null;

         if (Session != null && Session["TestFolder"] != null)
         {
            if (System.IO.File.Exists(Server.MapPath(VirtualPathUtility.ToAbsolute("~/Views" 
                                  + Session["TestFolder"].ToString().Trim() + "/" + ViewName + ".cshtml"))))
            {
                renderview = base.View("~/Views/" + Session["TestFolder"].ToString().Trim()+ "/" + ViewName + ".cshtml"
                                  , masterName, model);
            }
            else
            {
                renderview = base.View("~/Views/Shared/" + ViewName + ".cshtml", masterName, model);
            }
   
         }
         else
         {
             renderview = base.View("~/Views/Shared/" + ViewName + ".cshtml", masterName, model);
         }

         if (renderview != null)
              return renderview;


         return base.View(ViewName, masterName, model);
     }
}

As you can see in the above code i am overriding the method  "Viewof Controller class , which is been called on "return View()" in action. 

Note :  To get the value for "ViewName" parameter in the above method you have to make sure that you will pass the name of the view - return View("yourviewname") 

The above method searches the view in "AllViews" folder (Session["TestFolder"]) and in case view did not found in that location then it tries to search in "/Shared" folder.
For partial Views we have similar method -
protected override PartialViewResult PartialView(string viewName, object model)
{
 return base.PartialView(viewName, model);
}

So the main drawback of this approach is if in the view uses - "@HTML.Partial("partialviewname")" then both of the above methods would not be called then we need to go with approach of overriding razor view engine as I have shown in the last post.