部署 HttpHandler Web 服务

发布于 2024-09-02 08:30:46 字数 3384 浏览 5 评论 0原文

我正在尝试构建一个处理 http 请求 POST 和 GET 的 Web 服务。

这是一个示例:

public class CodebookHttpHandler: IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.HttpMethod == "POST")
        {
            //DoHttpPostLogic();
        }
        else if (context.Request.HttpMethod == "GET")
        {
            //DoHttpGetLogic();
        }
    }
...

public void DoHttpPostLogic()
{
...
}
public void DoHttpGetLogic()
{
...
}

我需要部署它,但我很难开始。大多数在线参考资料都显示制作一个网站,但实际上,我想做的就是在发送 HttpPost 时做出响应。我不知道要在网站中放置什么内容,只想运行该代码。

编辑: 我正在关注 网站,因为这正是我想要做的。

我已经设置了网站,我在 .cs 文件中有处理程序的代码,我已经编辑了 web.config 以添加我需要的文件扩展名的处理程序。现在,我处于第 3 步,您将向 IIS 告知此扩展并将其映射到 ASP.NET。另外,我使用的是 IIS 7,因此界面与屏幕截图略有不同。这是我遇到的问题:

1) Go to website
2) Go to handler mappings
3) Go Add Script Map
4) request path - put the extension I want to handle 
5) Executable-  it seems i am told to set aspnet_isapi.dll here. Maybe this is incorrect?
6) Give name
7) Hit OK button:

添加脚本映射

您想要允许此 ISAPI 扩展吗?单击“是”将带有“允许”条目的扩展添加到 ISAPI 和 CGI​​ 限制列表中,或者将 ISAPI 和 CGI​​ 限制列表中的现有扩展条目更新为“允许”。

否 取消


8) Hit Yes

添加脚本映射

此处理程序所需的指定模块不在模块列表中。如果要添加脚本映射处理程序映射,则 IsapiModule 或 CgiModule 必须位于模块列表中。

确定


编辑2:刚刚发现托管处理程序与托管代码中的处理程序有关,脚本映射是为了帮助配置可执行文件和模块映射以使其工作与 http 模块。所以我应该使用选项 1 - 添加托管处理程序。

我知道文件扩展名的请求路径是什么...并且我知道名称(可以随意称呼它),所以它一定是我正在努力解决的类型字段。到目前为止,在应用程序文件夹(在 IIS 中)中,我只有 MyHandler.cs 和 web.config (当然还有一个带有我试图为其创建处理程序的扩展名的文件!)

edit3: Progress

现在我已经有了代码并设置了 web.config,我测试一下是否可以浏览到 filename.CustomExtension 文件:

HTTP 错误 404.3 - 未找到 由于扩展配置的原因,无法提供您请求的页面。如果页面是脚本,请添加处理程序。如果应下载该文件,请添加 MIME 映射。

因此,在 IIS7 中,我转到处理程序映射并将其添加进去。 请参阅此 MSDN 示例,这正是我想要遵循的

该类如下所示:

using System.Web;

namespace HandlerAttempt2
{
    public class MyHandler : IHttpHandler
    {
        public MyHandler()
        {
            //TODO: Add constructor logic here
        }

        public void ProcessRequest(HttpContext context)
        {
            var objResponse = context.Response;
            objResponse.Write("<html><body><h1>It just worked");
            objResponse.Write("</body></html>");
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

    }
}

我按如下方式添加处理程序:

请求路径:*.whatever 类型:MyHandler(类名 - 根据示例,这看起来是正确的!) 名称:whatever

尝试再次浏览自定义文件(该文件在应用程序池中作为集成):

HTTP 错误 500.21 - 内部服务器错误 处理程序“whatever”在其模块列表中有一个错误的模块“ManagedPipelineHandler”

尝试再次浏览到自定义文件(这在应用程序池中为CLASSIC):

HTTP 错误 404.17 - 未找到 请求的内容似乎是脚本,并且静态文件处理程序不会提供服务。

直接问题

1) 网站需要处于经典模式还是集成模式?我在在线材料中没有找到任何对此的引用,无论它是否应该是。

2)我是否必须将 MyHandler.cs 编译为 .dll,或者我可以将其保留为。CS?它需要位于 bin 文件夹中,还是根目录中的任何位置?

I am trying to build a webservice that manipulates http requests POST and GET.

Here is a sample:

public class CodebookHttpHandler: IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.HttpMethod == "POST")
        {
            //DoHttpPostLogic();
        }
        else if (context.Request.HttpMethod == "GET")
        {
            //DoHttpGetLogic();
        }
    }
...

public void DoHttpPostLogic()
{
...
}
public void DoHttpGetLogic()
{
...
}

I need to deploy this but I am struggling how to start. Most online references show making a website, but really, all I want to do is respond when an HttpPost is sent. I don't know what to put in the website, just want that code to run.

Edit:
I am following this site as its exactly what I'm trying to do.

I have the website set up, I have the code for the handler in a .cs file, i have edited the web.config to add the handler for the file extension I need. Now I am at the step 3 where you tell IIS about this extension and map it to ASP.NET. Also I am using IIS 7 so interface is slightly different than the screenshots. This is the problem I have:

1) Go to website
2) Go to handler mappings
3) Go Add Script Map
4) request path - put the extension I want to handle 
5) Executable-  it seems i am told to set aspnet_isapi.dll here. Maybe this is incorrect?
6) Give name
7) Hit OK button:

Add Script Map

Do you want to allow this ISAPI extension? Click "Yes" to add the extension with an "Allowed" entry to the ISAPI and CGI Restrictions list or to update an existing extension entry to "Allowed" in the ISAPI and CGI Restrictions list.

Yes No Cancel


8) Hit Yes

Add Script Map

The specified module required by this handler is not in the modules list. If you are adding a script map handler mapping, the IsapiModule or the CgiModule must be in the modules list.

OK


edit 2: Have just figured out that that managed handler had something to do with handlers witten in managed code, script map was to help configuring an executable and module mapping to work with http Modules. So I should be using option 1 - Add Managed Handler.

I know what my request path is for the file extension... and I know name (can call it whatever I like), so it must be the Type field I am struggling with. In the applications folder (in IIS) so far I just have the MyHandler.cs and web.config (Of course also a file with the extension I am trying to create the handler for!)

edit3: progress

So now I have the code and the web.config set up I test to see If I can browse to the filename.CustomExtension file:

HTTP Error 404.3 - Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

So in IIS7 I go to Handler Mappings and add it in. See this MSDN example, it is exactly what I am trying to follow

The class looks like this:

using System.Web;

namespace HandlerAttempt2
{
    public class MyHandler : IHttpHandler
    {
        public MyHandler()
        {
            //TODO: Add constructor logic here
        }

        public void ProcessRequest(HttpContext context)
        {
            var objResponse = context.Response;
            objResponse.Write("<html><body><h1>It just worked");
            objResponse.Write("</body></html>");
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

    }
}

I add the Handler in as follows:

Request path: *.whatever
Type: MyHandler (class name - this appears correct as per example!)
Name: whatever

Try to browse to the custom file again (this is in app pool as Integrated):

HTTP Error 500.21 - Internal Server Error
Handler "whatever" has a bad module "ManagedPipelineHandler" in its module list

Try to browse to the custom file again (this is in app pool as CLASSIC):

HTTP Error 404.17 - Not Found
The requested content appears to be script and will not be served by the static file handler.

Direct Questions

1) Does the website need to be in CLASSIC or INTEGRATED mode? I don't find any reference of this in the online material, whether it should be either.

2) Do I have to compile the MyHandler.cs to a .dll, or can I just leave it as .cs? Does it need to be in a bin folder, or just anywhere in root?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

划一舟意中人 2024-09-09 08:30:46

RE 你的问题:

我不知道第一个问题的答案(经典或集成);但我可以帮助解决第二个问题...

是的,您需要先编译它。我从未尝试过将 dll 部署到 bin 以外的任何地方,因为这是标准,即使它确实有效,我也会怀疑将它们放在其他地方。

我部署 HttpHandlers 的方式非常简单直接 - 所有的艰苦工作都在 web.config 中完成,我从来不需要进入 IIS 来更改任何设置。

首先,要让 ASP.NET 处理 http 请求,您需要使用已通过管道传输到 ASP.NET 的请求后缀 - 例如 .aspx 或 ashx。如果您想使用其他东西,您将需要配置 IIS 来执行此操作,如上面的托管处理程序 img 所示。

我倾向于使用 .ashx 例如: http://localhost /foo/my/httphandler/does/this.ashx

您需要做的就是添加必要的配置(假设您已将 HttpHandler 编译为 DLL 并将其部署到站点)。

<configuration>
   <system.web>
       <httpHandlers>
            <add verb="*" 
                 path="*.ashx" 
                 type="MyApp.PublishingSystem.HttpHandlers.GroovyHandler, MyApp.PublishingSystem" />
       </httpHandlers>
   </system.web>
</configuration>

显然(?)您可以使用路径更改/限制范围,例如:

path="*.ashx" 
path="*ListWidgets.ashx" 
path="*Admin/ListWidgets.ashx" 

更多信息:http://msdn.microsoft.com/en-us/library/ms820032.aspx

需要注意的一个重要问题是在配置中声明 HttpHandler 的顺序;根据我的记忆,首先声明的优先。所以在这个例子中...

<add verb="*"  path="*foo.ashx" type="MyApp.PublishingSystem.HttpHandlers.FooHandler, MyApp.PublishingSystem" />
<add verb="*"  path="*.ashx" type="MyApp.PublishingSystem.HttpHandlers.GroovyHandler, MyApp.PublishingSystem" />

...groovy 处理程序将处理所有 HttpRequests除了任何以 foo.ashx 结尾的请求

顺便说一下,我在我的应用程序中使用了 HttpHanldrs开源 .net CMS / 应用程序框架,您可能会在那里找到一些有用的代码(?): http://morphfolia.codeplex .com/

RE your questions:

I don't know the answer to the first one (CLASSIC or INTEGRATED); but I can help with the second...

Yes you'll need to compile it first. I have never tried deploying dll's to anywhere other than the bin, given that that's the standard I would be suspect in putting them anywhere else even if it did work.

The way I deploy HttpHandlers is quiet straight forward - all the hard work's done in web.config, I'v enever had to go into IIS to change any settings.

For a start, for the http request to be handled by ASP.NET you need to use a request suffix that's already piped to ASP.NET - like .aspx or ashx. If you want to use something else you will need to config IIS to do this, as per your managed handler img above.

I tend to use .ashx e.g: http://localhost/foo/my/httphandler/does/this.ashx

All you need to do (assuming you've compiled athe HttpHandler into a DLL and deployed it to the site) is add the necessary config.

<configuration>
   <system.web>
       <httpHandlers>
            <add verb="*" 
                 path="*.ashx" 
                 type="MyApp.PublishingSystem.HttpHandlers.GroovyHandler, MyApp.PublishingSystem" />
       </httpHandlers>
   </system.web>
</configuration>

Obviously (?) you can change / restrict the scope using the path, e.g:

path="*.ashx" 
path="*ListWidgets.ashx" 
path="*Admin/ListWidgets.ashx" 

More info here: http://msdn.microsoft.com/en-us/library/ms820032.aspx

An important gotcha to look out for is the order in which you declare your HttpHandlers in the config; from what I remember ones declared first take precedent. So in this example...

<add verb="*"  path="*foo.ashx" type="MyApp.PublishingSystem.HttpHandlers.FooHandler, MyApp.PublishingSystem" />
<add verb="*"  path="*.ashx" type="MyApp.PublishingSystem.HttpHandlers.GroovyHandler, MyApp.PublishingSystem" />

...the groovy handler will handle all HttpRequests except any that end in foo.ashx

By the way, I make use of HttpHanldrs in my open source .net CMS / app framework, you might find some helpful code there (?): http://morphfolia.codeplex.com/

翻了热茶 2024-09-09 08:30:46

确保应用程序池的 .NET Framework 版本设置正确...

我在 .NET 2.0 应用程序池上部署了 .NET 4.0 Web 应用程序并收到此错误。将应用程序池设置为 v4.X,ashx 的表现就像冠军一样。

Make sure the app pool's .NET Framework Version is set correctly...

I deployed a .NET 4.0 web app on a .NET 2.0 app pool and got this error. Set the app pool to v4.X and the ashx was served like a champ.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文