【发布时间】:2017-12-12 05:05:45
【问题描述】:
我需要将自定义 httpHandler 添加到现有的 IIS 网站。我有一个带有 IIS 的 Windows Server 2012 R2,在 IIS 中我有一个运行 ASP.NET 解决方案的网站,我无法访问源代码。 ApplicationPool 配置为在 .Net 4.0 和集成模式下运行。
我们想开发一个自定义的 httpHandler 作为 .dll 并在网站的 Handler Mappings 下注册它。为此,我们在 Visual Studio 2015 中创建了一个新的动态链接库项目,代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace MinimalStandaloneHttpHandler
{
public class Class1 : IHttpHandler
{
public Class1()
{
}
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
// This handler is called whenever a file ending
// in .sample is requested. A file with that extension
// does not need to exist.
context.Server.Transfer("http://www.google.com", false);
}
public bool IsReusable
{
// To enable pooling, return true here.
// This keeps the handler in memory.
get { return false; }
}
}
}
我们已经编译了它,然后转到 IIS -> 网站 -> 处理程序映射 -> 添加通配符脚本映射。
这里我们添加了“*”作为请求路径、.dll 的完整路径和一个友好的名称。在 Handler Mappings -> My Handler -> Right click -> Request Restrictions -> Mapping -> Unchecked "Invoke handler only if request is mapped to:"。
处理程序现在列在启用的处理程序下。现在 web.config 被修改了:
<configuration>
<system.webServer>
<handlers>
<add name="asdasd" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\inetpub\wwwroot\WebSiteStaticTest\MinimalStandaloneHttpHandler.dll" resourceType="File" requireAccess="None" preCondition="bitness32" />
</handlers>
</system.webServer>
</configuration>
但是当我们在网站上执行页面时,处理程序似乎不起作用,因为我们没有被重定向到 Google。这里有什么问题?
【问题讨论】:
标签: c# iis httphandler