WebClient not returning a valid response

by joey.westcott 12. September 2009 19:44


A while back I was trying to consume some XML from a third party site and I found myself getting stuck on an error.  The site would not give me a valid response and would throw up a 403 error.  After digging around I found that I left off the user-agent info in the request header.  So the moral to this story is: Don't forget your user-agent!

Code Sample after the break.
More...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Development | .Net

I don't want no stinkin <span>

by joey.westcott 26. March 2009 15:34

Goal:
The label control adds <span> tags around the text when rendered, I needed to have these removed. I also show how to replace the <span> tag with whatever tag you would like.

I currently work on a site that has a custom built CMS, which is not always a good thing.  Now don't get me wrong there are some great ideas in this home grown CMS, but just like any application there are some wrong/bad things as well.  Here is one of those bad things that I had the time to fix recently. 

The web site content is entered via a WYSIWYG editor by whoever is responsible for the content, this markup (from the WYSIWYG) is stored in the database along with flags/keys that determine when to display this content.  If a few content items score the same then they all get built together, each one in its own Label control.  This is all fine and dandy, but when it comes time to dynamically create this content and add it to the current page is when they go less than ok.  Because of some limitations of the rest of the framework the content has to be built inside a System.Web.UI.WebControls.WebControl, ok this is fine except we are limited to a select few controls that fall into that category.

More...

Currently rated 3.8 by 4 people

  • Currently 3.75/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Development | .Net | Work

Category List bug in BlogEngine.Net 1.4.5.0

by joey.westcott 21. September 2008 11:17

I recently changed the layout of some of my categories in BlogEngine.Net 1.4.5.0.  In short I wanted to add a few parent Categories for the categories that I was already using.  For example: I have a category name ".net" that I lump all of my .net related items in but what if I also have some java code or C++ code and I have a category for them as well?  To me it makes sense to have a parent category called "development" and then if I want to view all code or development related items including C#, VB, Java, HTML, C/C++ then all I have to do is click on the new parent category and voila you have everything development related no matter what the sub-category is.

So for the bug:
When the Category List control/widget is rendered it looks at the Category.FullTitle to build the URI. This works fine if you are not a sub-category, like ".net" using the example above, but when rendering the URI for these sub-categories its using the FullTitle which is a concatenation of the parents categories and the title of the sub-category: ie: "development - .net".  This does not work and that link should really just be to the ".net" category. So here is the simple code fix that I used, but take in mind that you could also make it so that "development - .net" works.  I just found a quick and easy fix...

Code fix after the break.
More...

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Development | .Net

ImageButton's ImageUrl not resolving correctly

by joey.westcott 16. August 2008 18:29

I was working with an image button the other day and for the life of me I couldn't get rid of a runtime error when it went to load the control.  I had a dynamic URL that was something similar to this:

   1:  ImageUrl='../ImageHandler.ashx?File=<%#Eval("Path") %>&MaxSize=<%=PhotoMaxSize %>&ApplyWaterMark=<%=ApplyWatermark %>'

 

The problem with this is that when the .Net Framework goes to parse this property it first see the "../*" and then goes down a routine to parse the path out, BUT forgets about the fact that I might have something in the URL that needs to be resolved.  It did take me quite a while to figure out what was going on and why the "resolved " path was the same as what was between the quotes. 

My solution was to change it up just a little bit, here is what I ended up with:

   1:  ImageUrl='<%# string.Format("../ImageHandler.ashx?File={0}&MaxSize={1}&ApplyWaterMark={2}", Eval("Path"), PhotoMaxSize, ApplyWatermark) %>' 

The result is the same but I just used a little help from the oh so nice string.Format method. Now because the URL didn't start with "../" or even "~/" it looked and noticed that it needed to resolve what is in the <%#...%>.  On a side note this also applies to the other properties on the control and i would assume that other controls are handled the same way.

 

I don't know maybe I should have known this before now.

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Development | .Net

Monitor Your Websites With Sound

by joey.westcott 24. February 2008 13:20
This weekend I had some fun messing around with the idea of monitoring one of my sites via sounds. All of the sites I run are .Net 2.0 or .Net 3.5, with that it allowed me to create a .Net solution that would work with any of my sites.

The idea started with a single thought of "boy it would be kinda neat to hear a sound when someone viewed a page in my site." So I spent all of 30 mins figuring out what i wanted to do create. I built a simple library for playing sounds based on a file or a path to a file. I then took the new lib and built a simple HttpModule for attaching to a site and tapping into begin request event.

This could be used for monitoring a few different sites along with a few different request types, not to mention that you could also include a different sound for events like, app start, begin request, end request, a request for .* file types (ie: if you wanted to know when someone downloaded one of your .rar files, you could just play a different sound file.) I'm sure that you could prob add a hundred more options to this list if you really wanted to.

Anyway I enjoyed hearing all the hits to my different sites this weekend and thought that you might like to as well.


sound playing lib.
namespace YetAnotherDeveloper.Common.SoundUtility
{

    public
sealed class Player
    {
        private
Player() { }

        public
static void PlaySound(string filePath)
        {
            SoundPlayer player =
new SoundPlayer();
            player.SoundLocation = filePath;
            player.Play();
        }

    }

}


simple http module example.
namespace YetAnotherDeveloper.HttpModules
{
    public
class PlaySoundModule : IHttpModule
    {
        public
void Dispose() { }  

        //Tap into the events.

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

         void context_BeginRequest(object sender, EventArgs e)
        {
            YetAnotherDeveloper.Common.SoundUtility.Player.PlaySound(@"C:\Sounds\PageLoad.wav");
        }
    }
}


Modified webconfig to include this module in its pipeline.
<httpModules>
     <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    < add name="PlaySoundModule" type="YetAnotherDeveloper.HttpModules.PlaySoundModule, YetAnotherDeveloper.HttpModules"/>
</httpModules>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Development | .Net | Projects

My C# Quine

by joey.westcott 20. January 2008 10:51

I was just dorking it up on the net today and I found an old topic that I haven't thought about in a long time. 

The Quine: (wikipedia)
In short it is a program in which the output or result of the code is the output of the program itself.  So it really just is a program that's only goal is to output the source code of the program.  Many find it "fun" to see how short of a program they can create, for this reason most will leave out formatting including return/line breaks or anything else that is not required for the program to function.

After a little search on the topic I ran into this website that had an example of quines for a ton of different programming languages, but I didn't see any for C# or for that matter none for any .Net languages.  This is probably because the site is old and has not been updated in a long time.  So as I started to type in google "C# quine" I stopped myself and decided that I would just write my own... so I did.  As you can tell I didn't aim for the shortest possible quine, but this being my first run at this I just decided to keep it simple. Maybe I will redo this making the shortest one that I can.

First I'll show you the program with all the nice formatting that you are used to with Visual Studio, I have removed the formatting because I didn't want to include it in the output of the program.  I did include it at first and it was working just fine but it adds a lot of more junk that is just really not needed.

Code after the break
More...

Currently rated 4.8 by 6 people

  • Currently 4.833333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development

Using C# to dynamically change virtual directories in IIS

by joey.westcott 22. November 2007 19:51

At work I have a need to change where a virtual direcorty is mapped in IIS.  So being the lazy developer that I am, I wrote a program that helps me take care of this.  Its a simple application that runs in the system tray and allows the remapping of the virtual directory with a couple of clicks.  I have created a page that I will try to keep updated when and if things change with it.  The source code should be posted by the end of the weekend if anyone desires to have a look at it.

This program will create and delete an IIS virtual directory as needed in C#.   

More details here Virtual Directory Manager

Currently rated 4.0 by 5 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Development | .Net | Projects

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen