1、新建项目 》新建类库项目:HttmModules
在类文件实现代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Text.RegularExpressions;
namespace Imtech.SharePoint.Enhancement.HttpModules
{
public class RedirectModule : IHttpModule
{
#region IHttpModule Members
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string requestUrl = app.Request.Url.ToString();
Regex regEx = new Regex(@"^https?://.*(?<itemUrl>/[^/]+.[^/.]+)$");
if (regEx.IsMatch(requestUrl)) return;
if (!requestUrl.EndsWith("/", StringComparison.CurrentCulture)) requestUrl += "/";
string destinationUrl = String.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
using (SPSite site = new SPSite(requestUrl))
{
using (SPWeb web = site.OpenWeb())
{
if (PublishingWeb.IsPublishingWeb(web))
{
PublishingWeb publishingWeb =
PublishingWeb.GetPublishingWeb(web);
if (publishingWeb.DefaultPage.Url.EndsWith("/VariationRoot.aspx", StringComparison.CurrentCultureIgnoreCase))
{
string defaultPage = String.Empty;
using (SPWeb nlWeb = site.OpenWeb("nl"))
{
defaultPage = PublishingWeb.GetPublishingWeb(nlWeb).DefaultPage.Url;
}
destinationUrl = String.Concat(requestUrl, "nl/", defaultPage);
}
else
destinationUrl = String.Concat(requestUrl, publishingWeb.DefaultPage.Url);
}
else
destinationUrl = String.Concat(requestUrl, "default.aspx");
}
}
}
catch { }
});
if (!String.IsNullOrEmpty(destinationUrl))
{
app.Response.AddHeader("Location", destinationUrl); app.Response.StatusCode = 301;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Text.RegularExpressions;
namespace Imtech.SharePoint.Enhancement.HttpModules
{
public class RedirectModule : IHttpModule
{
#region IHttpModule Members
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string requestUrl = app.Request.Url.ToString();
Regex regEx = new Regex(@"^https?://.*(?<itemUrl>/[^/]+.[^/.]+)$");
if (regEx.IsMatch(requestUrl)) return;
if (!requestUrl.EndsWith("/", StringComparison.CurrentCulture)) requestUrl += "/";
string destinationUrl = String.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
using (SPSite site = new SPSite(requestUrl))
{
using (SPWeb web = site.OpenWeb())
{
if (PublishingWeb.IsPublishingWeb(web))
{
PublishingWeb publishingWeb =
PublishingWeb.GetPublishingWeb(web);
if (publishingWeb.DefaultPage.Url.EndsWith("/VariationRoot.aspx", StringComparison.CurrentCultureIgnoreCase))
{
string defaultPage = String.Empty;
using (SPWeb nlWeb = site.OpenWeb("nl"))
{
defaultPage = PublishingWeb.GetPublishingWeb(nlWeb).DefaultPage.Url;
}
destinationUrl = String.Concat(requestUrl, "nl/", defaultPage);
}
else
destinationUrl = String.Concat(requestUrl, publishingWeb.DefaultPage.Url);
}
else
destinationUrl = String.Concat(requestUrl, "default.aspx");
}
}
}
catch { }
});
if (!String.IsNullOrEmpty(destinationUrl))
{
app.Response.AddHeader("Location", destinationUrl); app.Response.StatusCode = 301;
}
}
#endregion
}
}
2、复制类库HttpModule.dll到网站bin目录下
3、在web.config中配置:
<httpModules>
<add name="ImtechRedirectModule" type="Imtech.SharePoint.Enhancement.HttpModules.RedirectModule" />
</httpModules>
<add name="ImtechRedirectModule" type="Imtech.SharePoint.Enhancement.HttpModules.RedirectModule" />
</httpModules>