ASP.NET is an awesome web development platform that many developers wouldn't ever want to part with. However, if given the option, there are definitely parts of the package that you may want to swap out. This article will show you how easy it can be to break away from the "inline-ASP" or "Web Forms" world, and delve into your own custom ViewEngine.
Bellow is the code that explains how to create your custom View Engine
Bellow is the code that explains how to create your custom View Engine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyViewEngine
{
public class customViewEngine : VirtualPathProviderViewEngine
{
public customViewEngine()
{
// Define the location of the View file
this.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.customview", "~/Views/Shared/{0}.customview" };
this.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.customview", "~/Views/Shared/{0}.customview" };
}
protected override IView CreatePartialView
(ControllerContext controllerContext, string partialPath)
{
var physicalpath = controllerContext.HttpContext.Server.MapPath(partialPath);
return new customView(physicalpath);
}
protected override IView CreateView
(ControllerContext controllerContext, string viewPath, string masterPath)
{
var physicalpath = controllerContext.HttpContext.Server.MapPath(viewPath);
return new customView(physicalpath);
}
}
}
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text.RegularExpressions;
namespace MyViewEngine
{
public class customView : IView
{
private string _viewPhysicalPath;
public customView(string ViewPhysicalPath)
{
_viewPhysicalPath = ViewPhysicalPath;
}
#region IView Members
public void Render(ViewContext viewContext, System.IO.TextWriter writer)
{
//Load File
string rawcontents = File.ReadAllText(_viewPhysicalPath);
//Perform Replacements
string parsedcontents = Parse(rawcontents, viewContext.ViewData);
writer.Write(parsedcontents);
}
#endregion
public string Parse(string contents, ViewDataDictionary viewdata)
{
return Regex.Replace(contents, "\\*(.+)\\*", m => GetMatch(m, viewdata));
}
public virtual string GetMatch(Match m, ViewDataDictionary viewdata)
{
if (m.Success)
{
string key = m.Result("$1");
if (viewdata.ContainsKey(key))
{
return viewdata[key].ToString();
}
}
return string.Empty;
}
}
}
------------------------------------------------------------------------------------------------------------
In Global.asax file you need to make this entry
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
//Register your View Engine Here. ViewEngines.Engines.Add(new customViewEngine());
}
------------------------------------------------------------------------------------------------------------
Create the controller and custom View Example: Views ->customView->index.customview
<html>
<head>
<title>Index MyView </title>
</head>
<body>*message*</body>
</html>
Here i am using * as instead of @ that we use in Razor Engine
No comments:
Post a Comment