gpt4 book ai didi

c# - 日志文件未从 HttpHandler 写入

转载 作者:行者123 更新时间:2023-11-30 15:13:07 25 4
gpt4 key购买 nike

我想捕获所有发送到 *.jpg 的请求我的服务器上的文件。为此,我创建了一个 HttpHandler,其代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Globalization;

namespace MyHandler
{
public class NewHandler : IHttpHandler
{
public NewHandler()
{}

public void ProcessRequest(System.Web.HttpContext ctx)
{
HttpRequest req = ctx.Request;
string path = req.PhysicalPath;
string extension = null;

string contentType = null;
extension = Path.GetExtension(path).ToLower();

switch (extension)
{
case ".gif":
contentType = "image/gif";
break;
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
default:
throw new NotSupportedException("Unrecognized image type.");
}

if (!File.Exists(path))
{
ctx.Response.Status = "Image not found";
ctx.Response.StatusCode = 404;
}
else
{
ctx.Response.Write("The page request is " + ctx.Request.RawUrl.ToString());
StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true);
sw.WriteLine("Page requested at " + DateTime.Now.ToString()
+ ctx.Request.RawUrl); sw.Close();

ctx.Response.StatusCode = 200;
ctx.Response.ContentType = contentType;
ctx.Response.WriteFile(path);
}
}

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

在编译并将其添加到我的网络应用程序的 Bin 之后目录作为引用,我在 web.config 中添加了以下内容文件:

<system.web>
<httpHandlers>
<add verb="*" path="*.jpg" type="MyHandler.NewHandler,MyHandler"/>
</httpHandlers>
</system.web>

然后我也修改了IIS设置Home Directory -> Application Configuration并添加了 aspnet_isapi.dll对于 .jpg扩展名。

在 Handler 中,我试图在我在 C 驱动器中创建的日志文件中写入一些内容,但它没有写入日志文件,而且我无法找到错误。

最佳答案

<httpHandlers>
<add verb="" path=".jpg" type="MyHandler.NewHandler,MyHandler"/>
</httpHandlers>

这只是stackoverflow的布局问题还是你省略了星号(*):

<httpHandlers>
<add verb="*" path="*.jpg" type="MyHandler.NewHandler,MyHandler"/>
</httpHandlers>

如果它没有缺少星号,我会尝试在 Firebug 中查看“网络”选项卡在 Firefox 上(或使用 Fiddler - 一个 http 调试代理)。您从调用中获得了什么 HTTP 响应代码?

关于c# - 日志文件未从 HttpHandler 写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/385945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com