Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a querystring into ashx file in c#

Tags:

c#

asp.net

I have been checking the internet and there is one answer here at stackoverflow, but it is in vb.net, and I am using c#,

k, this is the deal, I have a binary image stored in a sql server db. I have that working great, to load it in and also to retieve it. In a gridview, I have a link for the detail of the master/detail page. I am using a simple html image tag in the html part here is the code:

I am using VS2010 and C#

(displayDetail02.aspx)

<body>
<form id="form1" runat="server">
<div>
  <img id="Img1" width="500" 
               runat="server" 
               src="~/getLargeImage.ashx?Businessid=<%Eval(businessid)%>"
               alt="Business Image" 
               style="border: 1px inset"/>
</div>
</form>
</body>

Here is the code behind:

public partial class displayDetail02 : System.Web.UI.Page
{
   public string businessid = string.Empty;

   protected void Page_Load(object sender, EventArgs e)
   {
      if (!Page.IsPostBack)
      {
        businessid = Request.QueryString["businessid"];
      }
   }
}

(getLargeImage.ashx)

 public partial class getLargeImage : IHttpHandler {


 public void ProcessRequest(HttpContext context)
 {
    HttpContext _context = HttpContext.Current;
    context.Response.ContentType = "image/jpg";
    string businessid = Convert.ToString(context.Request.QueryString["businessid"]);    

  ...

My problem is the querystring variable, I have tried many many different ways to format the querystring coming from displayDetail02.aspx, but I can't seam to get the ' and " right to display the paramater, I keep getting '<%' which is the first part of query string.

I look at it in firebug in mozilla and the querystring is getting passed correctly, but it is not getting processed in the ashx file correctly.

I also tried it in the code behind etc, like here is some of the code I have tried.

<% --src='<%# "getLargeImage.ashx?Businessid=" + Eval("Businessid") %>' --%>

It is just one line of code ... oh, I know this works, because when I hardcode a parameter in the ashx (generic handler), I get the image back

Could someone please help me?

like image 352
Michael Salisbury Avatar asked Jun 19 '11 12:06

Michael Salisbury


2 Answers

I'm using ashx this way, try to use it as:

context.Request.Params["string"]
like image 153
Red Avatar answered Oct 23 '22 06:10

Red


protected void Page_Load(object sender, EventArgs e)
   {
      if (!Page.IsPostBack)
      {
        Img1.Src = "~/getLargeImage.ashx?Businessid=" + Request.QueryString["businessid"];
      }
   }
like image 4
Magnus Avatar answered Oct 23 '22 07:10

Magnus