我是否以错误的方式使用处理程序?

发布于 2024-08-25 09:45:47 字数 938 浏览 13 评论 0原文

我以前从未使用过 HTTP 处理程序,并且已经有一个可以工作,但我不确定我是否真正正确使用它。我已经生成了一个字符串,它将保存为 CSV 文件。当用户单击按钮时,我希望打开下载对话框,以便用户可以保存文件。我所拥有的有效,但我一直在阅读有关修改 web.config 文件的内容,但我不必这样做。

我的处理程序:

private string _data;
private string _title = "temp";


public void AddData(string data)
{
    _data = data;
}



public bool IsReusable
{
    get { return false; }
}

public void ProcessRequest(HttpContext context)
{

    context.Response.ContentType = "text/csv";
    context.Response.AddHeader("content-disposition","filename=" + _title + ".csv");
    context.Response.Write(_data);
    context.Response.Flush();
    context.Response.Close();

}

这是来自允许用户下载的页面: (单击按钮时)

string dataToConvert = "MYCSVDATA....";

csvHandler handler = new csvHandler();
handler.AddData(dataToConvert);

handler.ProcessRequest(this.Context);

这工作正常,但我见过没有实例化处理程序的示例,并且似乎总是修改 web.config。我做错了什么吗?

谢谢

I've never used HTTP Handlers before, and I've got one working, but I'm not sure if I'm actually using it properly. I have generated a string which will be saved as a CSV file. When the user clicks a button, I want the download dialog box to open so that the user can save the file. What I have works, but I keep reading about modifying the web.config file and I haven't had to do that.

My Handler:

private string _data;
private string _title = "temp";


public void AddData(string data)
{
    _data = data;
}



public bool IsReusable
{
    get { return false; }
}

public void ProcessRequest(HttpContext context)
{

    context.Response.ContentType = "text/csv";
    context.Response.AddHeader("content-disposition","filename=" + _title + ".csv");
    context.Response.Write(_data);
    context.Response.Flush();
    context.Response.Close();

}

And this is from the page that allows the user to download:
(on button click)

string dataToConvert = "MYCSVDATA....";

csvHandler handler = new csvHandler();
handler.AddData(dataToConvert);

handler.ProcessRequest(this.Context);

This works fine, but no examples I've seen ever instantiate the handler and always seem to modify the web.config. Am I doing something wrong?

Thanks

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

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

发布评论

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

评论(1

帅气尐潴 2024-09-01 09:45:47

在我看来,它根本没有真正利用它是一个处理程序这一事实。为实现您并不真正使用的接口创建一个单独的类,而不是将代码放在现有的页面类中,有什么好处?您只需对响应执行操作 - 那么为什么不在页面内执行此操作呢?

修改 web.config 的典型方法是因为您希望处理程序实际处理请求 - 而在您的情况下,页面正在接收请求,并且只是询问碰巧实现处理程序接口的对象将数据转储到响应中。

特别是,这个处理程序是否可以用作正常响应的独立处理程序?感觉只有当您调用 AddData 时它才会做任何有用的事情...如果您确实在 web.config 中注册了它,它只会响应一个空的 csv 文件(或者可能是一个例外),对吧?如果是这样的话,我真的不认为它应该首先实现 IHttpHandler - 它会误导其他稍后查看它的开发人员。

It seems to me that it's not really using the fact that it's a handler at all. What is the benefit in making a separate class for this implementing an interface you don't really use, rather than putting the code within your existing page class? You're only doing things to the response - so why not just do that within the page?

The typical approach of modifying web.config is because you want the handler to actually handle the request - whereas in your case the page is receiving the request, and just asking an object which happens to implement the handler interface to dump the data into the response.

In particular, would this handler be usable at all as a standalone handler for a normal response? It feels like it's only going to do anything useful when you've called AddData on it... if you did register it in web.config, it would just respond with an empty csv file (or possibly an exception), right? If that's the case, I really don't think it should implement the IHttpHandler in the first place - it will mislead other developers who look at it later.

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