----------------------------------------------------
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
// register handler for PreInit event
page.PreInit += new EventHandler(page_PreInit);
}
}
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
if (page != null)
{
SPSite currentSite = (SPSite)SPContext.Current.Site;
SPWeb currentWeb = currentSite.RootWeb;
if (page != null)
{
string strUserAgent = page.Request.UserAgent.ToString().ToLower();
Console.Write(strUserAgent);
if (strUserAgent != null)
{
if (page.Request.Browser.IsMobileDevice == true ||
strUserAgent.Contains("iphone") || strUserAgent.Contains("ipod") ||
strUserAgent.Contains("symbian") || strUserAgent.Contains("android") || strUserAgent.Contains("htc") ||
strUserAgent.Contains("windows ce") || strUserAgent.Contains("blackberry") ||
strUserAgent.Contains("palm") || (strUserAgent.Contains("mobile") && !strUserAgent.Contains("ipad")) ||
strUserAgent.Contains("opera mini"))
{
currentWeb.MasterUrl = "/_catalogs/masterpage/Iphone-Device.master";
currentWeb.CustomMasterUrl = "/_catalogs/masterpage/Iphone-Device.master";
}
else if (strUserAgent.Contains("mobile") && strUserAgent.Contains("ipad"))
{
currentWeb.MasterUrl = "/_catalogs/masterpage/Ipad-Device.master";
currentWeb.CustomMasterUrl = "/_catalogs/masterpage/Ipad-Device.master";
}
else
{
page.MasterPageFile = "/_catalogs/masterpage/Desktop-Device.master";
}
}
else
{
currentWeb.MasterUrl = "/_catalogs/masterpage/v4.master";
currentWeb.CustomMasterUrl = "/_catalogs/masterpage/v4.master";
}
currentWeb.AllowUnsafeUpdates = true;
currentWeb.Update();
currentWeb.AllowUnsafeUpdates = false;
}
}
}
public void Dispose() { /* empty implementation */ }