开发者

System.Web.HttpException: Response is not available in this context error

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Response is not available in this context.

Source Error: 


Line 10:    public void Download(string filename)
Line 11:    {
Line 12:         Response.ContentType = "application/exe";
Line 13:         Response.AppendHeader("Content-Disposition", filename);
Line 14:         Response.TransmitFile(Server.MapPath("~/Resources/bb.exe"));

I am having this method inside a class, i call this method when I click a button

using System;
using System.Collections.Gen开发者_开发百科eric;
using System.Linq;
using System.Web;

public class Common: System.Web.UI.Page
{
}

Where am I going wrong


When using the response object from an aspx page, its codebehind class or a user control, the response object is directly available because all these derive from the page object.

When using the response object in your own class, the object is not available, but you can access it:

HttpContext.Current.Response. -> something


You just can't use Response object of the Page like you're doing, because this object represents response which had been sent already, so you cannot modify it or use TransmitFile with it.

You need to create your own handler to write file to server output. Refer to IHttpHandler documentation (http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspx)


I'm not sure How,

But a broken connection string in my Web.config file - caused the same issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜