如何在我的实用程序库中创建一个返回响应的方法?
在代码隐藏中,我有一个包含以下内容的方法:
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "xxx";
Response.AppendHeader("Content-Disposition", "xxx");
Response.AppendHeader("Content-Length", xxx);
Response.BinaryWrite(xxx);
Response.Flush();
Response.End();
现在,此代码将在几个不同的地方使用。该代码在使用的各个地方都是相同的。只有文件名和字节数组会改变,这些是传递给现有方法的参数。
我的问题是,如何从当前代码隐藏中提取此代码并将其放入“实用程序”类中。我尝试将代码复制并粘贴到我希望它驻留在 Response 对象的类中,这给我带来了一些问题(无法解析符号“Response”)。我尝试实例化一个 HttpResponse 来返回,但构造函数只需要一个文本编写器,经过进一步研究,MSDN 说我无论如何都不应该这样做。
有什么建议吗?
In a code-behind I have a method that consists of the following:
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "xxx";
Response.AppendHeader("Content-Disposition", "xxx");
Response.AppendHeader("Content-Length", xxx);
Response.BinaryWrite(xxx);
Response.Flush();
Response.End();
Now, this code will be used in a few different places. The code will be identical in the various places it is used. Only the file name and byte array will change and those are parameters passed to the existing method.
My question is, how do I pull this code out from the current code-behind and place it in a "Utilities" class. I tried copying and pasting the code into the class where I'd like it to reside by the Response object is giving me some problems (Cannot resolve symbol 'Response'). I tried instantiating an HttpResponse to return but the constructor only takes a textwriter and upon further research, MSDN says I shouldn't be doing that anyway.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
HttpContext.Current.Response
,它将在您的 Utilities.cs 中工作Use
HttpContext.Current.Response
, which will work in your Utilities.cs