How To Access HTML Title Tag By IHttpHandler ?

as you see , we have HTML File And ASHX File .

The HTML File Containing Image Tag , The SRC Value Of The Image Is our ASHX File .

When We Open The HTML File The ASHX Creating Image On-fly Containing some text .we need to Access The real Title Tag Of The HTML Page and Print it On the Image instead The Static Text


1-askexpert.ashx
C Sharp Code

    public class specialimage : IHttpHandler
    {
       
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            string textToWrite = "Text Of Title Tag "; //we need to replace This Text with the real Title Of The page.
            if (textToWrite.Trim().Length > 0)
            {
                Bitmap image = new Bitmap(400, 30);
                Graphics g = null;            
                try            
                {                
                    g = Graphics.FromImage(image);                
                    Font f = new Font("Arial", 10, FontStyle.Regular);                
                    SolidBrush b = new SolidBrush(Color.White);                
                    g.FillRectangle(b, 0, 0, 400, 30);                
                    g.DrawString(textToWrite, f, Brushes.Blue, 2, 5);                
                    f.Dispose();                
                    image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);            
                }            
                catch (Exception ee)            
                {                
                    context.Response.Write(ee.Message.ToString());            
                }            
                finally            
                {                
                    image.Dispose();                
                    g.Dispose();            
                }        
            }    
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

2-index.htm ,
Containing one Image Element

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>This Is Index Page</title>
</head>
<body>
        <img alt="" src="specialimage.ashx" />
</body>
</html>

The SRC Value Is Our ASHX File .

alharbinAsked:
Who is Participating?
 
rodmjayConnect With a Mentor Commented:
or else you can create the same request with javascript if you arent using asp.net in the html
0
 
rodmjayConnect With a Mentor Commented:
the easiest way would be to pass it as a querystring var to the ashx, you dont have access to the same response object once you are within the IHttpHandler.

<img alt="" src="specialimage.ashx" />

change to:

<asp:image runat=server imageurl='<%= "specialimage.ashx?title=" + Server.UrlEncode(This.Title) %>'/>

then you can access that title from the hander via the request.querystring
0
 
alharbinAuthor Commented:
we need to access the title Tag directly from The Handler Code, we must avoid :
1-pass The Title as querystring.
2-Make New Request.

thank you.
0
 
rodmjayConnect With a Mentor Commented:
the http handler is a new request, and I do not believe you can access the page request/response in this manner
0
 
alharbinAuthor Commented:
i think you are worthy of Master , your reply is logical .
but i hope find a way to access HTML Tags from Server side script with or witout HTTP Handler class...

thank you again for help .

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.