繁体   English   中英

HTTP处理程序不适用于CS文件,但适用于ASHX文件

[英]http handler doesn't work in cs file but works in ashx file

我正在尝试运行http处理程序。 我有一个封闭的代码,当我将其直接放在ashx文件中时可以工作,但是当我将它们分开并放在cs文件中时,它就行不通了,我不确定为什么。

<%@ 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文件中时,它将无法正常工作:

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

另外,我的处理程序不会在每次运行查询时都运行。 它仅在我转到ashx文件并保存并运行查询时运行,它运行处理程序。 同样,我不确定为什么会这样。

任何建议将不胜感激。

提前致谢!!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM