Skip to main content

Microsoft Silverlight

Answered Question why my httpmodule rewrite iamge path wroks right at debug, but failed after published? RSS Feed

(0)

kkdhf
kkdhf

Member

Member

3 points

32 Posts

why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

follow is the code:

   public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpContext context = app.Context;
            if (IsIMG(context.Request.Url.LocalPath) == true)
            {
                if (context.Request.Url.LocalPath.EndsWith("NoImage.jpg") == false)
                {
                    if (File.Exists(context.Server.MapPath(context.Request.Url.LocalPath)) == false)
                    {
                        context.RewritePath("~/NoImage.jpg");
                    }
                }
            }
        }

Yes,I AM Chinese. And My Height More Than 1.74,
Reach 1.75.....Big Guy. :>

kkdhf
kkdhf

Member

Member

3 points

32 Posts

Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

...may be i should say more clear. i want to rewrite image path if the server doesn't exist the image.

and the result is when i debug it . it can get a right image if it's not exist.

but at server , seems it passed my code, but nothing happend.

what makes it not works at server? what should i do to make it works like what i want?

can u help me?

Yes,I AM Chinese. And My Height More Than 1.74,
Reach 1.75.....Big Guy. :>

tranduybien
tranduybien

Member

Member

186 points

53 Posts

Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

using try catch then MessageBox your error :)

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Regards,
Bien Tran Duy
http://infoway.com.vn

tranduybien
tranduybien

Member

Member

186 points

53 Posts

Re: Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

Check UriKind and change to UriKind.RelativeOrAbsolute in IsIMG function

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Regards,
Bien Tran Duy
http://infoway.com.vn

kkdhf
kkdhf

Member

Member

3 points

32 Posts

Re: Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

i have added try catch block,and catch nothing.seems it not occurs any exception.

and i use rewritepath("~/noimage.jpg"), i think it will works right , i often used it like this.

i have another module in the same website, it used it , works fine.

hope your reply . :)

Yes,I AM Chinese. And My Height More Than 1.74,
Reach 1.75.....Big Guy. :>

tranduybien
tranduybien

Member

Member

186 points

53 Posts

Re: Re: Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

try this one: if (context.Request.Url.LocalPath.ToUpterCase().EndsWith("NOIMAGE.JPG") == false)

MessageBox.Show(context.Request.Url.LocalPath); to check valid link

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Regards,
Bien Tran Duy
http://infoway.com.vn

kkdhf
kkdhf

Member

Member

3 points

32 Posts

Re: Re: Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

in fact , IsIMG is follow:

  private bool IsIMG(string url)
        {
            url = url.ToLower();
            string con = "(.gif|.bmp|.png|.jpg|.jpeg)$";
            Regex regex = new Regex(con);
            return regex.IsMatch(url);
        }

and it seems filter other link.

Yes,I AM Chinese. And My Height More Than 1.74,
Reach 1.75.....Big Guy. :>

tranduybien
tranduybien

Member

Member

186 points

53 Posts

Answered Question

Re: Re: Re: Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

Try to check with :

            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ImageUrl);
                request.Timeout = 5000; // 5 seconds in milliseconds
                request.ReadWriteTimeout = 20000; // allow up to 20 seconds to elapse
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return response.GetResponseStream();
            }
            catch
            {
                return null;
            }

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Regards,
Bien Tran Duy
http://infoway.com.vn

kkdhf
kkdhf

Member

Member

3 points

32 Posts

Re: Re: Re: Re: why my httpmodule rewrite iamge path wroks right at debug, but failed after published?

in fact , i tried to return Image Bytes to response stream , but always failed at client. i think ,maybe IIS give a 404 error before i rewrite the image path. at last, i add javascript code at every place needed. ... ...

Yes,I AM Chinese. And My Height More Than 1.74,
Reach 1.75.....Big Guy. :>
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities