Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.Generic;
using System.Linq;
using System.Web;

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

Where am I going wrong

like image 732
John Avatar asked Sep 27 '11 13:09

John


3 Answers

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

like image 117
Lysgaard Avatar answered Nov 18 '22 13:11

Lysgaard


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)

like image 28
Sergei B. Avatar answered Nov 18 '22 14:11

Sergei B.


I'm not sure How,

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

like image 2
itsho Avatar answered Nov 18 '22 14:11

itsho