<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8584375829200993501</id><updated>2011-06-30T03:03:04.847-07:00</updated><title type='text'>Interesting one..</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sd-interestingone.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8584375829200993501/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sd-interestingone.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>SD</name><uri>http://www.blogger.com/profile/12390393630567665845</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8584375829200993501.post-4467378150002580639</id><published>2009-01-15T01:51:00.000-08:00</published><updated>2009-01-15T02:22:56.641-08:00</updated><title type='text'>How to: Host ASP.Net MVC site under a SharePoint.</title><content type='html'>&lt;span style="font-family: verdana; font-size: 85%;"&gt;&lt;br /&gt;We had requirement to host simple asp.net site under SharePoint site. To host a simple asp.net website is SharePoint site is pretty straight forward. But as we were using ASP.Net MVC framework for our website, we faced few challenges as below: &lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;APS.Net MVC uses its own view engine i.e.VirtualPathProviderViewEngine, this view engine uses the hosting site VirtualPathProvider. So in our case it was using sharepoint’s VirtualPathProvider. SP’s virtual path provider was not able to understand the MVC view’s path. &lt;br /&gt;To resolve this issue we created our own custom view engine dervied from MVC’s view engine. Now the problem was to tell the application to use our custom view engine. This is resolved by registering new view engines during Application_Start.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;MVC uses its own http module and routing model (UrlRoutingModule), need to register this in the web.config of sharepoint site, so that all MVC views are handled by the MVC modules and handlers.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Cannot have session state, authentication in web.config of MVC application. As the MVC is application set as a subsite (new virtual dir under SP site) and not a website it will use session and authentication of its parent site hence our MVC application web config cannot have these keys.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;And finally we were successful to host asp.net MVC site under sp site.Below are the steps to run your asp.net MVC site under SP site.&lt;br /&gt;Steps to host a MVC site under SP site:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;In your MVC application create a class for your custom view engine (CustomViewEngine.cs)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Derive CustomViewEngine class from VirtualPathProviderViewEngine and implement this abstrct class. Major changes to the view engine class as below:&lt;br /&gt;&lt;div style="background-color:#FFCC99;"&gt;&lt;br /&gt;//Constructor of custom view engine &lt;br /&gt;public CustomViewEngine()&lt;br/&gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;// This is where we tell MVC where to look for our files. &lt;br /&gt;&amp;nbsp;// This says&lt;br /&gt;&amp;nbsp;// to look for a file at "Views/Controller/Action.html"&lt;br /&gt;&amp;nbsp;ViewLocationFormats = new[] &lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; "~/MVCApp/Views/{1}/{0}.aspx",&lt;br /&gt;&amp;nbsp;&amp;nbsp; "~/MVCApp/Views/{1}/{0}.ascx", &lt;br /&gt;&amp;nbsp;&amp;nbsp;"~/MVCApp/Views/Shared/{0}.aspx",&lt;br /&gt;&amp;nbsp;&amp;nbsp;"~/MVCApp/Views/Shared/{0}.ascx"&lt;br /&gt;&amp;nbsp;};&lt;br /&gt;&amp;nbsp;PartialViewLocationFormats = ViewLocationFormats;&lt;br /&gt;&amp;nbsp;//Add your master page location here..&lt;br /&gt;&amp;nbsp;base.MasterLocationFormats = base.MasterLocationFormats;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;//Method that fetches the path from general name. This is private method used by GetPath() method of the view engine.&lt;br /&gt;&amp;nbsp;private string GetPathFromGeneralName(string[] locations, string name, string controllerName, ref string[] searchedLocations)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;string result = String.Empty;&lt;br /&gt;      &amp;nbsp;&amp;nbsp;searchedLocations = new string[locations.Length];&lt;br /&gt;      &amp;nbsp;&amp;nbsp;for (int i = 0; i &lt; locations.Length; i++)&lt;br /&gt; &amp;nbsp;&amp;nbsp;{&lt;br /&gt;         &amp;nbsp;&amp;nbsp;&amp;nbsp;string virtualPath = String.Format(CultureInfo.InvariantCulture, locations[i], name, controllerName);&lt;br /&gt;         &amp;nbsp;&amp;nbsp;&amp;nbsp;searchedLocations = _emptyLocations;&lt;br /&gt;         &amp;nbsp;&amp;nbsp;&amp;nbsp;result = virtualPath;&lt;br /&gt;       &amp;nbsp;&amp;nbsp;}&lt;br /&gt;       &amp;nbsp;return result;&lt;br /&gt;  &amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;//Method that fetches the path from specific name. This is private method used by GetPath() method of the view engine.&lt;br /&gt;&amp;nbsp;  private string GetPathFromSpecificName(string name, ref string[]  searchedLocations)&lt;br /&gt;  &amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;string result = name;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;return result;&lt;br /&gt;  &amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;//Override the methods in VirtualPathProviderViewEngine.&lt;br /&gt;  &amp;nbsp;protected override IView CreatePartialView(ControllerContext  controllerContext, string partialPath)&lt;br /&gt;  &amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;return new WebFormView(partialPath, null);  &lt;br /&gt;  &amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;protected override IView CreateView(ControllerContext  controllerContext, string viewPath, string masterPath)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;return new WebFormView(viewPath, masterPath);   &lt;br /&gt;  &amp;nbsp;}&lt;br /&gt; &lt;/div&gt;&lt;br /&gt;      &lt;br /&gt;   &lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Give strong name to your MVC application and put the dll in GAC.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Create virtual directory under your SharePoint site.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Point virtual directory to your MVC application.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Copy httpHandlers, httpModules, assemblies from MVC configuration file to SP configuration file.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Remove authentication, sessionState, customErrors from MVC configuration file.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;In global.asax of SP site add below code:&lt;br /&gt;     &lt;br /&gt;   &lt;div style="background-color:#FFCC99;"&gt;&lt;br /&gt;    &amp;nbsp;void Application_Start(object sender, EventArgs e) &lt;br /&gt;      &amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;RegisterRoutes(RouteTable.Routes);&lt;br /&gt;     &amp;nbsp;&amp;nbsp;ViewEngines.Engines.Clear();&lt;br /&gt;     &amp;nbsp;&amp;nbsp;ViewEngines.Engines.Add(new CustomViewEngine());&lt;br /&gt;    &amp;nbsp;} &lt;br /&gt;   &lt;br /&gt;    &amp;nbsp;public static void RegisterRoutes(RouteCollection routes)&lt;br /&gt;    &amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;Route MyRoute = new Route("MVCApp/{controller}/{action}/{id}", new MvcRouteHandler());&lt;br /&gt;     &amp;nbsp;&amp;nbsp;MyRoute.Defaults = new RouteValueDictionary();&lt;br /&gt;     &amp;nbsp;&amp;nbsp;MyRoute.Defaults.Add("controller", "Home" );&lt;br /&gt;     &amp;nbsp;&amp;nbsp;MyRoute.Defaults.Add("action", "Index" );&lt;br /&gt;     &amp;nbsp;&amp;nbsp;MyRoute.Defaults.Add("id", "");&lt;br /&gt;     &amp;nbsp;&amp;nbsp;routes.Add(MyRoute);&lt;br /&gt;    &amp;nbsp;}&lt;br /&gt;    &amp;nbsp;MVCApp --&gt; This is the name of your MVC virtual dir.        &lt;br /&gt;   &lt;/div&gt;&lt;br /&gt;   &lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Reset IIS and browse the application.&lt;/li&gt;&lt;br /&gt;  &lt;/ol&gt;&lt;br /&gt;   &lt;b&gt;&lt;u&gt;Appendix: Complete custom view engine class&lt;/u&gt;&lt;/b&gt; &lt;br /&gt;  &lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;   &lt;div style="font-family:verdana;font-size:85%;background-color:#FFCC99;"&gt;&lt;br /&gt;   &amp;nbsp;public class CustomViewEngine : VirtualPathProviderViewEngine &lt;br /&gt;   &amp;nbsp;{&lt;br /&gt;    &amp;nbsp;&amp;nbsp;private static readonly string[] _emptyLocations = new string[0];&lt;br /&gt;    &amp;nbsp;&amp;nbsp;public CustomViewEngine()&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;    &amp;nbsp;&amp;nbsp;&amp;nbsp;// This is where we tell MVC where to look for our files. &lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;  // to look for a file at "Views/Controller/Action.html"&lt;br /&gt;    &amp;nbsp;&amp;nbsp;&amp;nbsp; ViewLocationFormats = new[] {&lt;br /&gt;        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;"~/MVCApp/Views/{1}/{0}.aspx",&lt;br /&gt;                &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;"~/MVCApp/Views/{1}/{0}.ascx", &lt;br /&gt;                &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;"~/MVCApp/Views/Shared/{0}.aspx",&lt;br /&gt;                &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;"~/MVCApp/Views/Shared/{0}.ascx"&lt;br /&gt;            &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;&amp;nbsp; PartialViewLocationFormats = ViewLocationFormats;&lt;br /&gt;                       &lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;//Add your master page location here..&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;base.MasterLocationFormats = base.MasterLocationFormats;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;if (controllerContext == null)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new ArgumentNullException("controllerContext");&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;if (string.IsNullOrEmpty(partialViewName))&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;             &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new ArgumentException("Provide partial view name"&lt;br /&gt;             , "partialViewName");&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;string[] searched;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;string controllerName = controllerContext.RouteData.GetRequiredString("controller");&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;string partialPath = GetPath(PartialViewLocationFormats,  "PartialViewLocationFormats", partialViewName,controllerName,  out searched);&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;if (String.IsNullOrEmpty(partialPath))&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;           &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new ViewEngineResult(searched);&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;return new  ViewEngineResult(CreatePartialView(controllerContext,  partialPath), this);&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;public override ViewEngineResult FindView(ControllerContext  controllerContext, string viewName, string masterName)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;if (controllerContext == null)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;{&lt;br /&gt;               &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new ArgumentNullException("controllerContext");&lt;br /&gt;     &amp;nbsp;&amp;nbsp;}&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;if (String.IsNullOrEmpty(viewName))&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;               &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new ArgumentException("View name is missing", "viewName");&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;        &amp;nbsp;&amp;nbsp;&amp;nbsp;string[] viewLocationsSearched;&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;string[] masterLocationsSearched;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;string controllerName = controllerContext.RouteData.GetRequiredString("controller");&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;string viewPath = GetPath(ViewLocationFormats, "ViewLocationFormats", viewName, controllerName, out viewLocationsSearched);&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;string masterPath = GetPath(MasterLocationFormats, "MasterLocationFormats", masterName, controllerName, out masterLocationsSearched);&lt;br /&gt;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;if (String.IsNullOrEmpty(viewPath) || (String.IsNullOrEmpty(masterPath) &amp;&amp;  !String.IsNullOrEmpty(masterName)))&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new  ViewEngineResult(viewLocationsSearched.Union(masterLocationsSearched));&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;     &lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this);&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;private string GetPath(string[] locations, string   locationsPropertyName, string name, string controllerName, out  string[] searchedLocations)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;searchedLocations = _emptyLocations;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;if (String.IsNullOrEmpty(name))&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return String.Empty;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;        &amp;nbsp;&amp;nbsp;&amp;nbsp;if (locations == null || locations.Length == 0)&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;       &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICul ture, "Locations property name is missing",  locationsPropertyName));&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;bool nameRepresentsPath = IsSpecificPath(name);&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;return (nameRepresentsPath) ?&lt;br /&gt;                GetPathFromSpecificName(name, ref searchedLocations) :&lt;br /&gt;                GetPathFromGeneralName(locations, name,  controllerName, ref searchedLocations);&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;private string GetPathFromGeneralName(string[] locations, string name, string controllerName, ref string[] searchedLocations)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;string result = String.Empty;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;searchedLocations = new string[locations.Length];&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;for (int i = 0; i &lt; locations.Length; i++)&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string virtualPath = String.Format(CultureInfo.InvariantCulture, locations[i], name,  controllerName);&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;searchedLocations = _emptyLocations;&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result = virtualPath;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;return result;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;private string GetPathFromSpecificName(string name, ref string[]  searchedLocations)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string result = name;&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return result;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;private static bool IsSpecificPath(string name)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;char c = name[0];&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return (c == '~' || c == '/');&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;    &lt;br /&gt;     &amp;nbsp;&amp;nbsp;protected override IView CreatePartialView(ControllerContext  controllerContext, string partialPath)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new WebFormView(partialPath, null);  &lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;protected override IView CreateView(ControllerContext  controllerContext, string viewPath, string masterPath)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new WebFormView(viewPath, masterPath);   &lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;   &amp;nbsp;}&lt;br /&gt;  &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8584375829200993501-4467378150002580639?l=sd-interestingone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sd-interestingone.blogspot.com/feeds/4467378150002580639/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sd-interestingone.blogspot.com/2009/01/how-to-host-aspnet-mvc-site-under.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8584375829200993501/posts/default/4467378150002580639'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8584375829200993501/posts/default/4467378150002580639'/><link rel='alternate' type='text/html' href='http://sd-interestingone.blogspot.com/2009/01/how-to-host-aspnet-mvc-site-under.html' title='How to: Host ASP.Net MVC site under a SharePoint.'/><author><name>SD</name><uri>http://www.blogger.com/profile/12390393630567665845</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8584375829200993501.post-1914648715103779104</id><published>2009-01-13T02:10:00.001-08:00</published><updated>2009-01-29T03:13:08.554-08:00</updated><title type='text'>Improve performance of Regular expressions by using complied regular expressions</title><content type='html'>&lt;span style="font-family: verdana; font-size: 85%;"&gt;&lt;br /&gt;In one of our projects, we had convert given output file to a given specific format. Which included large nuumber fo string operations and number of regular expressions to execute over many different strings. Our project was completed functionally then later we faced huge performnace issues. As we were using complex regular expression to find the match and convert to given format and it was consuming huge time.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: verdana; font-size: 85%;"&gt;&lt;br /&gt;Then we came across the article which mentioned about the complied regular expressions. Using complied regular expressions the timings came to half of the original timings.&lt;br /&gt;Lets see here what are and how to use complied regular expressions..&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: verdana; font-size: 85%;"&gt;&lt;br /&gt;If an expression is not compiled, the regular expression engine converts the expression to a series of internal codes that are recognized by the regular expression engine; it is not converted to MSIL. As the expression runs against a string, the engine interprets the series of internal codes. This can be a slow process, especially as the source string becomes very large and the expression becomes much more complex. &lt;br /&gt;Compiling regular expressions allows the expression to run faster. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There are two ways to compile regular expressions. The easiest way is to use the &lt;span style="color:Navy"&gt;RegexOptions.Compiled&lt;/span&gt; enumeration value in the &lt;span style="color:Navy"&gt;Options&lt;/span&gt; parameter of the static &lt;span style="color:Navy"&gt;Match&lt;/span&gt; or &lt;span style="color:Navy"&gt;Matches&lt;/span&gt; methods on the &lt;span style="color:Navy"&gt;Regex&lt;/span&gt; class. And other is to precompile all of these expressions into their own assembly.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: verdana; font-size: 85%;"&gt;&lt;br /&gt;Lets go with first option: &lt;br /&gt;&lt;br /&gt;We can use the &lt;span style="color:Navy"&gt;RegexOptions.Compiled&lt;/span&gt; enumeration value in the Options parameter of the static &lt;span style="color:Navy"&gt;Match&lt;/span&gt; or &lt;span style="color:Navy"&gt;Matches&lt;/span&gt; methods on the &lt;span style="color:Navy"&gt;Regex&lt;/span&gt; class, shown as below:&lt;br /&gt;&lt;/span&gt; &lt;br /&gt;&lt;div style="font-family: verdana; font-size: 85%; background-color:#FFCC99;"&gt;&lt;br /&gt;Match objMatch = Regex.Match(inputString, pattern, RegexOptions.Compiled); &lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-family: verdana; font-size: 85%;"&gt;&lt;br /&gt;There is one drwaback of this option that is: an in-memory assembly gets generated to contain the IL, which can never be unloaded. An assembly can never be unloaded from an AppDomain. The garbage collector cannot remove it from memory. If large numbers of expressions are compiled, the amount of heap resources that will be used up and not released will be larger. So use this technique wisely. &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="font-family: verdana; font-size: 85%;"&gt;&lt;br /&gt;The second option is: &lt;br /&gt;&lt;br /&gt;Precompiling all of these expressions into their own assembly.Compiling regular expressions into their own assembly immediately gives you two benefits. First, precompiled expressions do not require any extra time to be compiled while your application is running. Second, they are in their own assembly and therefore can be used by other applications. &lt;br /&gt;&lt;br /&gt;To compile one or more expressions into an assembly, the static &lt;span style="color:Navy"&gt;CompileToAssembly&lt;/span&gt; method of the &lt;span style="color:Navy"&gt;Regex&lt;/span&gt; class must be used. To use this method, a &lt;span style="color:Navy"&gt;RegexCompilationInfo&lt;/span&gt; array must be created and filled with &lt;span style="color:Navy"&gt;RegexCompilationInfo&lt;/span&gt; objects. The next step is to create the assembly in which the expression will live. An instance of the &lt;span style="color:Navy"&gt;AssemblyName&lt;/span&gt; class is created using the default constructor. Next, this assembly is given a name (do not include the .dll file extension in the name, it is added automatically). Finally, the &lt;span style="color:Navy"&gt;CompileToAssembly&lt;/span&gt; method can be called with the &lt;span style="color:Navy"&gt;RegexCompilationInfo&lt;/span&gt; array and the &lt;span style="color:Navy"&gt;AssemblyName&lt;/span&gt; object supplied as arguments. &lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8584375829200993501-1914648715103779104?l=sd-interestingone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sd-interestingone.blogspot.com/feeds/1914648715103779104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sd-interestingone.blogspot.com/2009/01/improve-performance-of-executing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8584375829200993501/posts/default/1914648715103779104'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8584375829200993501/posts/default/1914648715103779104'/><link rel='alternate' type='text/html' href='http://sd-interestingone.blogspot.com/2009/01/improve-performance-of-executing.html' title='Improve performance of Regular expressions by using complied regular expressions'/><author><name>SD</name><uri>http://www.blogger.com/profile/12390393630567665845</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8584375829200993501.post-4739597751506404643</id><published>2009-01-13T00:36:00.000-08:00</published><updated>2009-01-13T01:37:03.768-08:00</updated><title type='text'>What is HttpModule..When and How  to write own custom http module.</title><content type='html'>&lt;p&gt;&lt;span style="font-family:verdana;font-size:85%;"&gt;You might have heard of HTTP pipelines, HTTP Handlers and HTTP Modules in past but do we really know what they are? It was same with me a few months before. We make a web request n number of times in a day and rarely thing of what the number of stages the request has gone through since we often keep it as an abstraction. Finally when I realized importance of HTTP Modules I decided to write an article about how someone should go about this.&lt;br /&gt;This article will start with a brief description of HTTP protocol and understanding what HTTP Modules are, where HTTP Module fit, then what HTTP Modules are and a brief description of when and how to use.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:verdana;"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;&lt;u&gt;The ASP.NET HTTP Pipeline&lt;br /&gt;&lt;/u&gt;&lt;/strong&gt;To understand the role of HTTP modules in ASP.NET page life cycle, you need to understand how the HTTP protocol works. Once an HTTP request comes in over port 80 (the regular HTTP port or port 443 for HTTPS and the secure sockets layer), the request passes through a number of stages making up the HTTP pipeline before it's actually fielded by your application.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana;font-size:85%;"&gt;Request --&gt; IIS --&gt; ASPNET_ISAPI.DLL --&gt; HttpApplication- (HttpModule)-&gt; HttpHandler&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana;font-size:85%;"&gt;IIS is the first participant in the chain. IIS is used to route the request to the ASP.NET runtime. IIS maps the ASP.NET file extensions to ASPNET_ISAPI.DLL - an ISAPI extension provided with ASP.NET. The job of ASPNET_ISAPI.DLL is to forward the request to the ASP.NET worker process, ASPNET_WP.EXE. From that point on, the request is wrapped up into an instance of HttpContext and piped through a number of ASP.NET classes.&lt;br /&gt;The next step for the request is to pass through an instance of HttpApplication. This stage is useful for maintaining application-scope methods, data, and events.&lt;br /&gt;After the HttpApplication object massages the request, it pushes the request through one or more HttpModule objects which play a role of providing pre- and post-processing on each request.&lt;br /&gt;There are a number of system-level HTTP modules, providing services ranging from authentication to state management to output caching. The number of modules that get to intercept the request is based upon settings within the host machine's machine.config file and the application's web.config file. In classic ASP, this role of providing pre- and post-processing fell upon ISAPI filters. It turns out that ASP HTTP modules are more straightforward to write.&lt;br /&gt;The final piece of the chain is the HttpHandler. If you've been working with ASP.NET for a while, you're familiar with the System.Web.UI.Page class. The Page class is an HttpHandler object, implementing the interface IHttpHandler. Classes implementing IHttpHandler can hook into the HTTP pipeline and service requests through the interface's ProcessRequest method.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana;"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;&lt;u&gt;HTTP Modules&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;One of ASP.NET's most useful features is the extensibility of the HTTP pipeline - the path that data takes between client and server. You can use them to extend your ASP.NET applications by adding pre- and post-processing to each HTTP request coming into your application. For example, if you wanted custom authentication facilities for your application, the best technique would be to intercept the request when it comes in and process the request in a custom HTTP module.&lt;br /&gt;An HTTP module is simply a class that implements the System.Web.IHttpModule interface:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;div style="background-color:#FFCC99;"&gt;&lt;span style="font-family:verdana;font-size:85%;"&gt;&lt;br /&gt;&lt;br /&gt;Public interface IHttpModule&lt;br&gt; {&lt;br&gt; void Dispose(); &lt;br&gt; void Init(HttpApplication context);&lt;br&gt; }&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;span style="font-family:verdana;font-size:85%;"&gt;When an HttpModule is hooked into the pipeline (via an entry in web.config), the ASP.NET runtime calls the module's Init and Dispose methods. Init is a window where the HTTP Module can initialize itself. These methods are invoked by the ASP.NET runtime when a module gets registered Init method gets executed and when unregister Dispose method gets executed. The Init and Dispose methods represent the module's opportunity to hook into a variety of events exposed by HttpApplication. These events include the beginning of a request, the end of a request, a request for authentication, and so forth. Notice the HttpApplication parameter passed in to the Init method. Usually, the Init method takes the HttpApplication object and maps event handlers to the desired events.&lt;br /&gt;Following is a C# code for defining a simple custom HttpModule that attaches to an application's BeginRequest and EndRequest events to provide simple pre- and post-processing before and after each request.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;div style="background-color:#FFCC99;font-family:Verdana;font-size:85%;"&gt;&lt;br /&gt;namespace HttpModuleExamples &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;&amp;nbsp;public class CustomHttpModule : IHttpModule&lt;br&gt;&lt;br /&gt;&amp;nbsp;{&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// IHttpModule memberspublic &lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;void Init(HttpApplication httpApp)&lt;br&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;httpApp.BeginRequest += new EventHandler(this.OnBeginRequest);&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;httpApp.EndRequest += new EventHandler(this.OnEndRequest);&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public void Dispose() &lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Usually, nothing has to happen here...&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// event handlers &lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public void OnBeginRequest(object o, EventArgs ea) &lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;HttpApplication httpApp = (HttpApplication) o;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;HttpContext ctx = HttpContext.Current;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ctx.Response.Write("Beginning Request ");&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public void OnEndRequest(object o, EventArgs ea)&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;HttpApplication httpApp = (HttpApplication) o; &lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;HttpContext ctx = HttpContext.Current;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ctx.Response.Write("Ending Request ");&lt;br&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&lt;br /&gt;&amp;nbsp;}&lt;br&gt;&lt;br /&gt;} &lt;br&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;To wire this handler up into the processing chain, the web.config file simply declares the module within the httpModules section, as shown here:&lt;/span&gt;&lt;/p&gt;&lt;div style="background-color: #FFCC99; font-family: Verdana; font-size: 85%;"&gt;&lt;br /&gt;  &amp;lt;configuration&amp;gt;&lt;br /&gt;  &lt;br&gt;&lt;br /&gt;  &amp;nbsp;&amp;lt;system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;nbsp;&amp;nbsp;&amp;lt;httpModules&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;add type= "HttpModuleExamples.CustomHttpModule, HTTPModules" name="CustomHttpModule"&lt;br /&gt;   /&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;nbsp;&amp;nbsp;&amp;lt;/httpModules&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;nbsp;&amp;lt;/system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;/configuration&amp;gt;&lt;br /&gt;&lt;br /&gt;  &lt;/div&gt;&lt;p&gt;&lt;span style="font-family:verdana;font-size:85%;"&gt;&lt;strong&gt;&lt;u&gt;H&lt;/u&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="font-family:verdana;"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;&lt;u&gt;TTP Handlers&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;HTTP handler’s process the request and are generally responsible for initiating necessary business logic tied to the request. Custom handlers must implement the System.Web.IHttpHandler interface. Additionally, a handler factory can be created which will analyze a request to determine what HTTP handler is appropriate. Custom handler factories implement the System.Web.IHttpHandlerFactory interface.&lt;br /&gt;When to use Modules and Handlers&lt;br /&gt;With all of the options available in ASP.NET, it is sometimes hard to determine what solution best fits your needs. Of course, it's always best to keep things simple; but, you still need to take evolutionary considerations and experience levels of current and future team members who have a potential of working on the project into account.&lt;br /&gt;Consider what it is that you want to do within your module or handler. Some functions, such as authentication can be added within modules. Modules should be considered only when there is a need for pass-through and/or intercepting interaction with the request. Alternatively, handlers should be put in place when there is a need to direct functional actions to one or more parts of an application.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:verdana;font-size:85%;"&gt;Conclusion&lt;br /&gt;HTTP modules are very beneficial and can be complex but these can solve problems that could be very difficult to solve by any other way. &lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8584375829200993501-4739597751506404643?l=sd-interestingone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sd-interestingone.blogspot.com/feeds/4739597751506404643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sd-interestingone.blogspot.com/2009/01/what-is-httpmodulewhen-and-how-to-write.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8584375829200993501/posts/default/4739597751506404643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8584375829200993501/posts/default/4739597751506404643'/><link rel='alternate' type='text/html' href='http://sd-interestingone.blogspot.com/2009/01/what-is-httpmodulewhen-and-how-to-write.html' title='What is HttpModule..When and How  to write own custom http module.'/><author><name>SD</name><uri>http://www.blogger.com/profile/12390393630567665845</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
