3-8 SharePoint 开发自定义的HttpModule

新建web应用程序空项目

 

 

 

添加 ASP.NET 模块

 

using Microsoft.SharePoint;
using System;
using System.Web;

namespace ljzHttpModule
{
    public class ljzModuleDemo : IHttpModule
    {
        /// <summary>
        /// 您将需要在网站的 Web.config 文件中配置此模块
        /// 并向 IIS 注册它,然后才能使用它。有关详细信息,
        /// 请参阅以下链接: https://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule 成员

        public void Dispose()
        {
            //此处放置清除代码。
        }

        public void Init(HttpApplication context)
        {
            // 下面是如何处理 LogRequest 事件并为其 
            // 提供自定义日志记录实现的示例
            //context.LogRequest += new EventHandler(OnLogRequest);
            context.EndRequest += Context_EndRequest; ;

        }

        private void Context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            app.Context.Response.Write("12332");
            if (app.Context.Request.Url.ToString().IndexOf("Pages") > 0)
            {
                string currentName = SPContext.Current.Web.CurrentUser.Name;
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList list = web.Lists["List"];
                        SPListItem item = list.Items.Add();
                        item["Title"] = currentName + " - " + DateTime.Now.ToString();
                        item.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            }
            //throw new NotImplementedException();
        }

        #endregion

        public void OnLogRequest(Object source, EventArgs e)
        {
            //可以在此处放置自定义日志记录逻辑
        }
    }
}

复制dll到bin目录

 

 

注册到sharepoint  webconfig配置

 

 

 

 

  <add name="ljzModuleDemo" type="ljzHttpModule.ljzModuleDemo,ljzHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

 

posted @ 2020-05-28 18:18  七秒钟得记忆  阅读(122)  评论(0)    收藏  举报