mvc使用IHttpHandler,得到500错误

时间:2015-01-05 15:17:45

标签: c# asp.net asp.net-mvc

我的应用程序根目录中有以下处理程序(.ashx)文件:

    public class KeepSessionAlive : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Session["KeepSessionAlive"] = DateTime.Now;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

我已将以下内容添加到我的RouteConfig.cs:

            routes.IgnoreRoute("KeepSessionAlive.ashx");

我还将以下内容添加到我的web.config中: 在system.web下 - > HttpHandlers的:

<add verb="*" path="/KeepSessionAlive.ashx" type="MyWeb.KeepSessionAlive" validate="false" />

并在system.webserver下 - &gt;处理

<add name="KeepSessionAlive" path="/*.ashx" verb="*" type="MyWeb.KeepSessionAlive" />

我一直遇到的问题是网站一直在寻找页面所在的同一文件夹中的处理程序,而不是根目录。例如,如果我点击主页,在Fiddler中我看到了:

GET /MySite/KeepSessionAlive.ashx?c=678962070347 HTTP/1.1

返回200并起作用。但是,如果我转到子文件夹页面,例如/ MySite / Payment / Edit,当我在Fiddler中留下编辑页面时,我会看到这一点:

GET /EAOTracking/Payment/EditAM/KeepSessionAlive.ashx?c=776502292135 HTTP/1.1

然后返回500错误,其中指出: System.ArgumentException:参数字典包含参数&#39; id&#39;的空条目。非可空类型的System.Int32&#39; for method&#39; System.Web.Mvc.ActionResult Edit(Int32)&#39;在&#39; MyWeb.Controllers.PaymentController&#39;。可选参数必须是引用类型,可空类型,或者声明为可选参数。参数名称:参数

我的编辑操作定义如下:

public ActionResult Edit(int id)

我不确定为什么Handler总是在根文件夹中找不到,因为如果我在网站的根文件夹中,它就可以工作。

1 个答案:

答案 0 :(得分:1)

如果您需要在aboslute网址上调用页面 - 请使用绝对网址

 "/KeepSessionAlive.ashx"

在页面上使用相对url(“KeepSessionAlive.ashx”)(我假设你使用JavaScript来ping处理程序)将它与页面的基本URL结合起来。