Silverbolt logo and banner
 

ASP.NET HttpRequest.Url Values

September 2nd, 2010

When developing in ASP.NET (C# or VB.NET), I always forget what values the different properties of HttpRequest.Url (which is a Uri object) return. Here is a list of all the values it returns.

NOTE: This applies to ASP.NET 4.0, so some Properties may not be available in older versions.

For the example Url:

http://www.silverbolt.net/Projects/DeploymentTool/Screenshots.php?TestParam=1

Property Description Example Value
AbsolutePath Gets the absolute path of the URI. /Projects/DeploymentTool/Screenshots.php
AbsoluteUri Gets the absolute URI. http://www.silverbolt.net/Projects/DeploymentTool/Screenshots.php?TestParam=1
Authority Gets the Domain Name System (DNS) host name or IP address and the port number for a server. www.silverbolt.net
DnsSafeHost Gets an unescaped host name that is safe to use for DNS resolution. www.silverbolt.net
Fragment Gets the escaped URI fragment.
Host Gets the host component of this instance. www.silverbolt.net
HostNameType Gets the type of the host name specified in the URI. Dns
IsAbsoluteUri Gets whether the Uri instance is absolute. true
IsDefaultPort Gets whether the port value of the URI is the default for this scheme. true
IsFile Gets a value indicating whether the specified Uri is a file URI. false
IsLoopBack Gets whether the specified Uri references the local host. false
IsUnc Gets whether the specified Uri is a universal naming convention (UNC) path. true
LocalPath Gets a local operating-system representation of a file name. /Projects/DeploymentTool
OriginalString Gets the original URI string that was passed to the Uri constructor. http://www.silverbolt.net/Projects/DeploymentTool/Screenshots.php?TestParam=1
PathAndQuery Gets the AbsolutePath and Query properties separated by a question mark (?). /Projects/DeploymentTool/Screenshots.php?TestParam=1
Port Gets the port number of this URI. 80
Query Gets any query information included in the specified URI. ?TestParam=1
Scheme Gets the scheme name for this URI. http
Segments Gets an array containing the path segments that make up the specified URI. {string[4]}
[0]: “/”
[1]: “Projects/”
[2]: “DeploymentTool/”
[3]: “Screenshots.php”
UserEscaped Indicates that the URI string was completely escaped before the Uri instance was created. false
UserInfo Gets the user name, password, or other user-specific information associated with the specified URI.

Deployment Tool v1.0.1 Available For Download

February 23rd, 2010

The Deployment Tool has been updated to v1.0.1. Download it here.

A summary of the changes in this release are as follows:

  • Added a help file to the installer.
  • The installer now detects and overwrites older versions.
  • Improved the ‘About’ page to include a URL to SilverBolt.net.

Deployment Tool v1.0.0 Available For Download

February 15th, 2010

The Deployment Tool is a free application that simplifies the deployment of web applications from their source location to the live server. It automatically detects which files will be changed on the latest deployment and allows you to back up those files before copying your new website across.

The first version of this application is available to download for free from here.

Any and all feedback regarding the tool is welcome.


MySQL Connection Timeouts with PHP and Windows Vista

December 19th, 2009

I am using PHP v5.3.1 on my development machine with MySQL v5.1.11 and Apache 2.2.

Whenever I tried to make a connection to MySQL through any PHP script, I was getting extremely slow response times, or sometimes no response at all. In some cases all I was getting was a blank white screen – no error messages, nothing in the PHP error log, nothing in the Apache err_log either. Not helpful.

I created a basic PHP MySQL connection file as follows:

<?php
// TEST Database Connection

/*--- Database Details ---*/
$dbhost   = 'localhost;
$dbuser   = 'root';
$dbpass   = 'mypass';
$databaseSchema = 'mydatabase';

$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die ('Error connecting to mysql');

mysql_select_db($databaseSchema);
mysql_close($conn);

?>

When I tried to load this test file, I waited for 30 seconds, and eventually I got the following error message:

Warning: mysql_connect() [function.mysql-connect]: [2002] A connection
attempt failed because the connected party did not (trying to connect
via tcp://localhost:3307)

My MySQL server was installed and working correctly on this port number, so what as going on? This post had the answer. Windows Vista (and probably Windows 7 too) is unable to resolve the hostname ‘localhost’ to ‘127.0.0.1′. The solution was to change the $dbhost value in all my PHP scripts from ‘localhost’ to ‘127.0.0.1′.

This fixed all the problems immediately.


Setup PHP/MySQL/Apache/XDebug/Eclipse For Windows

December 16th, 2009

Here’s how I setup Apache, MySQL, PHP on Windows Vista Home Premium. This assumes you are only intending to use your machine as a development machine (i.e. no remote connections).

NOTE: I am using the 64-bit version of Windows, but there are only 32-bit versions of most of these components anyway. Basically, wherever a 64-bit version of a component is available, it is advisable to get that instead of a 32-bit version.

Setup Apache Web Server

  1. Download the best available version of Apache from http://httpd.apache.org/download.cgi. Get the Win32 binary with installer (not the “Win32 Source” version).
  2. Install the file you just downloaded. You can change the installation directory, but leave everything else at their default settings (unless you have a particular need to change something and you know what you’re doing!).
  3. Configure the following settings in the Apache Configuration File (httpd.conf):
    1. Modify the ‘ServerName’ directive to match your computer name and port number. By default, the port number is 80 (unless you changed it) and you can use ‘localhost’ as the computer name:
      ServerName localhost:80
    2. Modify the DocumentRoot to point to the directory you wish to use as your website’s home:
      DocumentRoot "D:/Websites"
    3. Modify the <Directory /> tag to point to the same directory as DocumentRoot:
      <Directory "D:/Websites">
    4. If you are only intending to use Apache for development purposes on your local machine, you can restrict it to only accept connections from your local machine by modifying the ‘Listen’ directive as follows:
      Listen 127.0.0.1:80
    5. If you wish to browse directories through your web browser, you can enable fancy directory listings by uncommenting/adding the following:
      Include conf/extra/httpd-autoindex.conf
  4. Test your configuration and start the Apache server. You can go to your website through your browser by browsing to:
    http://localhost:80

Setup MySQL

  1. Download the Generally Available (GA) Release of MySQL Community Server from http://dev.mysql.com/downloads.
  2. Install MySQL from the file you just downloaded. Use all the default options (use the quick setup if you like).
  3. You’ll probably also want to download the MySQL GUI Tools too: http://dev.mysql.com/downloads/gui-tools/5.0.html.

Setup PHP

  1. Download the latest Thread Safe PHP Windows Installer package from: http://windows.php.net/download.
  2. Install PHP using the file you just downloaded.
    1. Select to install the PEAR libraries and documentation.
    2. During the installation yo uwill be asked to select the location of the Apache Configuration File. This is typically found here:
      [Apache Installation Directory]/conf/httpd.conf
  3. After installation, you will need to configure the php.ini file ready for development:
    1. Enable the option to display errors in PHP (if it is not already set):
      display_errors = On
    2. Add (or modify) the following lines to enable SMTP mail:
      SMTP = your.smtp.server
      smtp_port = 25
      ; For Win32 only.
      sendmail_from = yourname@domain.com
  4. Ensure PHP file handlers are added to the Apache httpd.conf file (they should be added by the PHP installer in step 2.2):
    PHPIniDir "C:/Program Files (x86)/PHP/"
    LoadModule php5_module "C:/Program Files (x86)/PHP/php5apache2_2.dll"
  5. Create a test PHP file (called test.php) in your chosen web server home directory, which contains the following line:
    <?php phpinfo(); ?>
  6. Save the file, and then browse to the file at:
    http://localhost/test.php
  7. If everything has gone well, you should have a page full of PHP information telling you every PHP parameter for your current installation.

Setup XDebug

  1. Download the latest Windows binary appropriate for the version of PHP you downloaded earlier (e.g. VC6 (32 bit)).
  2. Copy the downloaded file to the PHP extension directory:
    [PHP Installation Directy]/ext
  3. Edit the php.ini file to include the following lines (Note: substitute the right path/filename for your xdebug dll file):
    [XDebug]
    ;; Only Zend OR (!) XDebug
    ; zend_extension="C:\xampp\php\ext\php_xdebug.dll"
    ; Modify the filename below to reflect the .dll version of your xdebug
    zend_extension="C:\Program Files (x86)\PHP\ext\php_xdebug-2.0.5-5.3-vc6.dll"
    xdebug.remote_enable=true
    xdebug.remote_host=127.0.0.1
    xdebug.remote_port=9000
    xdebug.remote_handler=dbgp
    xdebug.profiler_enable=0
    xdebug.profiler_output_dir="C:\Program Files (x86)\PHP\output"
  4. Restart the Apache web server and reload your test.php page. It should now include an additional ‘XDebug’ section.

Setup PHP Eclipse IDE

  1. Download the PHP Development Tools Eclipse IDE (PDT) All-in-one for Windows (32-bit) from: http://www.eclipse.org/pdt/downloads.
  2. Unzip the downloaded file to a temporary directory and then move the resulting ‘eclipse’ directory to a permanent location.
  3. Execute the eclipse/eclipse.exe file.
  4. A ‘Workspace Launcher’ dialog box will appear asking you to choose your Workspace directory. Just stick with the default and click OK.
  5. In the ‘Welcome’ screen, click on the ‘Workbench’ icon on right side towards the middle.
  6. In the Workbench, change to the PHP Perspective by clicking ‘Window’->’Open Perspective’->’PHP’.
  7. To be continued…

Here’s how I setup Apache, MySQL, PHP on Windows Vista Home Premium. This assumes you are only intending to use your machine as a development machine (i.e. no remote connections).

NOTE: I am using the 64-bit version of Windows, but there are only 32-bit versions of most of these components anyway. Basically, wherever a 64-bit version of a component is available, it is advisable to get that instead of a 32-bit version.

Setup Apache Web Server

  1. Download the best available version of Apache from http://httpd.apache.org/download.cgi. Get the Win32 binary with installer (not the “Win32 Source” version).
  2. Install the file you just downloaded. You can change the installation directory, but leave everything else at their default settings (unless you have a particular need to change something and you know what you’re doing!).
  3. Configure the following settings in the Apache Configuration File (httpd.conf):
    1. Modify the ‘ServerName’ directive to match your computer name and port number. By default, the port number is 80 (unless you changed it) and you can use ‘localhost’ as the computer name:
      ServerName localhost:80
    2. Modify the DocumentRoot to point to the directory you wish to use as your website’s home:
      DocumentRoot "D:/Websites"
    3. Modify the <Directory /> tag to point to the same directory as DocumentRoot:
      <Directory "D:/Websites">
    4. If you are only intending to use Apache for development purposes on your local machine, you can restrict it to only accept connections from your local machine by modifying the ‘Listen’ directive as follows:
      Listen 127.0.0.1:80
    5. If you wish to browse directories through your web browser, you can enable fancy directory listings by uncommenting/adding the following:
      Include conf/extra/httpd-autoindex.conf
  4. Test your configuration and start the Apache server. You can go to your website through your browser by browsing to:
    http://localhost:80

Setup MySQL

  1. Download the Generally Available (GA) Release of MySQL Community Server from http://dev.mysql.com/downloads.
  2. Install MySQL from the file you just downloaded. Use all the default options (use the quick setup if you like).
  3. You’ll probably also want to download the MySQL GUI Tools too: http://dev.mysql.com/downloads/gui-tools/5.0.html.

Setup PHP

  1. Download the latest Thread Safe PHP Windows Installer package from: http://windows.php.net/download.
  2. Install PHP using the file you just downloaded.
    1. Select to install the PEAR libraries and documentation.
    2. During the installation yo uwill be asked to select the location of the Apache Configuration File. This is typically found here:
      [Apache Installation Directory]/conf/httpd.conf
  3. After installation, you will need to configure the php.ini file ready for development:
    1. Enable the option to display errors in PHP (if it is not already set):
      display_errors = On
    2. Add (or modify) the following lines to enable SMTP mail:
      SMTP = your.smtp.server
      smtp_port = 25
      ; For Win32 only.
      sendmail_from = yourname@domain.com
  4. Ensure PHP file handlers are added to the Apache httpd.conf file (they should be added by the PHP installer in step 2.2):
    PHPIniDir "C:/Program Files (x86)/PHP/"
    LoadModule php5_module "C:/Program Files (x86)/PHP/php5apache2_2.dll"
  5. Create a test PHP file (called test.php) in your chosen web server home directory, which contains the following line:
    <?php phpinfo(); ?>
  6. Save the file, and then browse to the file at:
    http://localhost/test.php
  7. If everything has gone well, you should have a page full of PHP information telling you every PHP parameter for your current installation.

Setup XDebug

  1. Download the latest Windows binary appropriate for the version of PHP you downloaded earlier (e.g. VC6 (32 bit)).
  2. Copy the downloaded file to the PHP extension directory:
    [PHP Installation Directy]/ext
  3. Edit the php.ini file to include the following lines (Note: substitute the right path/filename for your xdebug dll file):
    [XDebug]
    ;; Only Zend OR (!) XDebug
    ; zend_extension="C:\xampp\php\ext\php_xdebug.dll"
    ; Modify the filename below to reflect the .dll version of your xdebug
    zend_extension="C:\Program Files (x86)\PHP\ext\php_xdebug-2.0.5-5.3-vc6.dll"
    xdebug.remote_enable=true
    xdebug.remote_host=127.0.0.1
    xdebug.remote_port=9000
    xdebug.remote_handler=dbgp
    xdebug.profiler_enable=0
    xdebug.profiler_output_dir="C:\Program Files (x86)\PHP\output"
  4. Restart the Apache web server and reload your test.php page. It should now include an additional ‘XDebug’ section.

Setup PHP Eclipse IDE

To be continued…


Reference ASP.NET Master Page Members from Content Pages

November 8th, 2009

I created a master page in my ASP.NET 3.5 project, and needed to access some of it’s custom properties from one of the content pages that referenced it. How do you access the member properties of a master page programmatically?

Most websites will tell you to simply do this:

  1. On your .aspx page, include the following: <%@ MasterType virtualpath="~/myMasterPage.master" %>
  2. To reference the members, simply type Master.Customer or Master.YourProperty from your CodeBehind page.

This wasn’t the whole story for me though. Step (2) didn’t work. When I tried to access the custom member variables of my Master Page, they weren’t visible using the above method. So, in addition to the above recommendations, I also had to cast my reference to the Master Page itself.

My master page has the following class:

public partial class ManageProfile : System.Web.UI.MasterPage
{
...

So in my CodeBehind page, I cast the Master page as a ManageProfile class:

((ManageProfile)Master).YourProperty;

And it works OK now.


GigTripper News Updates

October 14th, 2009

Subscribe to this blog for any updates to the GigTripper project.


Deployment Tool News Updates

October 14th, 2009

Subscribe to this blog to be kept updated as to any updates to the Deployment Tool project.


Slow Network Transfers With TortoiseSVN

October 11th, 2009

I had a problem with TortoiseSVN being incredibly slow with file transfers from the local SubVersion server. The symptoms were as follows:

  1. An extremely long delay (around 20-30 seconds) when entering any screen that interacts with the SVN server (e.g. Repository Browser, Show Log, etc…). The windows wait cursor would wait and wait and wait.
  2. Extremely slow file transfers when updating, committing or exporting anything from the repository to the local disk. File transfers were coming down no faster than 10 kb/sec.

No other programs were affected, and even browsing the repository through the web browser was nice and quick. Internet download speeds and windows network file transfers were nice and quick. So the problem was specific to either TortoiseSVN itself, or the SVN browser. I had done the following to try and correct the problem (without success):

  1. Updated all Ethernet drivers.
  2. Applied all Windows updates.
  3. Updated TortoiseSVN to the latest version (twice).
  4. Ran disk utilities (scandisk, etc…).
  5. Stripped out all unnecessary background applications.
  6. Modified the MTU settings.
  7. Removed and freshly exported all SVN projects.
  8. Removed and freshly generated all security certificates (for https:// SVN transfers)

Having upgraded TortoiseSVN twice, and seen no improvement, the only thing left that could be causing the problem was the SVN server itself. And yet, my colleague here in work was not experiencing any problems with it. Nevertheless we upgraded the Operating System which the SVN server was on from Windows Server 2003 to Windows Server 2008. The result? Nothing – still the same problems. I was ready to re-format my computer and start again when our network administrator quite by accident noticed something odd about the SVN server. A tracert and ping to it would fail at the command line, despite the fact that it was clearly working within the Windows XP shell. So we did an nslookup on the server and got the following information display:

Server: UnKnown
Address: 192.168.1.117

Name: svnserver.ourdomain.com
Address: 192.168.1.115, 192.168.3.57

Our SVN server appeared to be using 2 IP addresses, one of which was not responding at all. I checked my colleague’s computer and his was showing the same thing – 2 IP addresses! And yet his was working OK.

But his IP addresses were being displayed in a different order to mine. The working IP address (192.168.3.57) was displayed first on his computer, and second on mine.

The order of the IP addresses is critical. My computer was always checking (and waiting) for the first IP address to respond. When it timed out (after around 30 seconds), it then tried the second address which instantly responded. My colleague’s computer always responded when checking the first address, so never had any need to switch to the alternative address.

It turns out that the second non-working IP address was a hangover from a recent server relocation. The network administrator had to sort out the Network configuration properly on the server and a quick restart later the server was only displaying one working IP address to nslookup requests.

The result? Tortoise SVN instantly started working correctly again. Not even a reboot was necessary.

Thank goodness for serendipity.


Read HTML Source C#

October 11th, 2009

To open a remote web page and read its source into a string using ASP.NET and C#, do this:

HttpWebRequest newRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
HttpWebResponse webResponse = (HttpWebResponse)newRequest.GetResponse();
System.IO.StreamReader stream = new System.IO.StreamReader(webResponse.GetResponseStream());
Console.WriteLine(stream.ReadToEnd());
stream.Close();
webResponse.Close();

SilverBolt Development is powered by WordPress. Entries (RSS) and Comments (RSS).