http处理程序在cs文件中不起作用,但在ashx文件中有效

时间:2015-12-09 20:23:11

标签: c# .net visual-studio-2008 sharepoint-2007

我正在尝试运行http处理程序。我有一个封闭的代码,当我把它直接放在ashx文件中时可以工作但是当我把它们分开并放在cs文件中然后它不起作用而且我不确定原因。

for ... in

当我将代码分开并将包含的代码留在ashx文件中时,它就不起作用了:

<%@ WebHandler Language="C#"  Class="TheCityofCalgary.GSA.SharePoint.GSAClick" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Configuration;
using System.Net;
using System.Runtime.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Configuration;

namespace TheCityofCalgary.GSA.SharePoint
{
    /// <summary>
    /// a generic http handler to redirect the ClickLog to GSALoaction that is not accessible from the public
    /// </summary>
    public class GSAClick : IHttpHandler
    {

        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        private const string GSA_LOCATION_KEY = "GSALocation";

        public static StreamWriter LogSW = null;

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        /// <summary>
        /// Process incoming request and redirect to GSALocation
        /// </summary>
        /// <param name="context"></param>
       public void ProcessRequest(HttpContext context)
        {

            try
            {
                WebRequest _webRequest = WebRequest.Create(gsaLocation);
                _webRequest.BeginGetResponse(new AsyncCallback(RespCallback), null);
            }
            catch (Exception ex)
            {

                Console.WriteLine("Exception GSAClick:");
                Console.WriteLine("------------------------------");
                Console.WriteLine(ex.Message);
                Console.WriteLine("Stack trace:");
                Console.WriteLine(ex.StackTrace);               
                if(!string.IsNullOrEmpty(ex.StackTrace))
                    Log("Statck Trace: "+ex.StackTrace, true, "info");

            }

        }



        #endregion

        #region Private
        /// <summary>
        /// Required for making a async call to GSALocation but not required for response
        /// </summary>
        /// <param name="ar"></param>
        private static void RespCallback(IAsyncResult ar)
        {
            //do nothing
        }
        #endregion

    }
}

此外,每次运行查询时,我的处理程序都不会运行。它仅在我转到ashx文件并保存然后运行查询时运行,它运行处理程序。我不知道为什么会这样。

任何建议都将不胜感激。

提前致谢!!

1 个答案:

答案 0 :(得分:0)

在项目中添加对程序集TheCityofCalgary.GSA.SharePoint的引用后,它工作了。