in Search

Blog Day Afternoon

Take a walk on the mild side.

December 2008 - Posts

  • URL Rewriting using Intelligencia UrlRewriter Example 2

    I provided more detailed installation instructions for Intelligencia's UrlRewriter.dll in my previous post URL Rewriting using Intelligencia UrlRewriter Example 1.This post will concentrate on a different implementation. This implementation consisted of a website where each member of the website would have their own URL to a web page containing web parts that were customizable by each member. The previous example consisted by a website with store categories and products. All links on the pages were generated by the system and therefore always had a full URL out to the page name, such as default.aspx. These well-formed page requests were easily handled by the URL Rewriter. In this example, a well-formed URL would be http://www.samplesite.com/JoeExample/default.aspx. Unfortunately, we can't always control how members would type in their own URL. This system has had to be prepared to handle the cases where a member would type in http://www.samplesite.com/JoeExample/ or http://www.samplesite.com/JoeExample (both are missing the page name default.aspx). The well-formed URL is handled by the URL Rewriter by putting the following code in the web.config file.

    <rewriter>
      <unless url="~/admin/(.+)">
        <unless url="~/member/(.+)">
          <rewrite url="~/(.+)/Default.aspx" to="~/member/Default.aspx" processing="stop" />
        </unless>
      </unless>
    </rewriter>

    I added the <unless> tags to skip any request going to my admin subdirectory and to skip any request that was being directed to the member subdirectory. If a request for "/JoeExmple/Default.aspx" came in, the system would re-direct them to the member directory. Note that I didn't have to add any querystring parameters to the re-written URL. This system uses ASPNet Authentication so by the time the visitor arrived in the member directory, they were already logged in and the system knew who they were.

    I did not want to create empty stub directories for every member. This causes a problem if the visitor types in just /JoeExample or /JoeExample/ without any page name on the end. The directory does not exist so the system was going to my "Page Not Found" web page. To handle cases like this, I added the following code to my error page.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      'rewrite url of the form "/user" or "/user/" to "/user/default.aspx"
      Dim strMember
    As String = Request.ServerVariables("QUERY_STRING").ToLower.Replace(".aspx", "")
      If Right(strMember, 1) = "/" Then strMember = Left(strMember, strMember.Length - 1) 'remove last "/"
     
    Dim intPos As Integer = strMember.LastIndexOf("/")
     
    If intPos > -1 Then strMember = Right(strMember, Len(strMember) - intPos - 1) Else strMember = ""
     
    If strMember > "" Then Response.Redirect("http://www.samplesite.com/" & strMember & "/Default.aspx")
    End Sub

    If a member name is found in the requested URL, then a well-formed URL is generated and the visitor is re-directed to it.

    Posted Dec 19 2008, 01:50 PM by Blogette with 2 comment(s)
    Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web
  • URL Rewriting using Intelligencia UrlRewriter Example 1

    Search engine friendly URLs have become more important. It's not very useful to have all of your store products, for example, have the same URL with the only difference being in the querystring. A URL such as http://www.storename.com/product.aspx?cat=123&prod=456 is not very intuitive. My favorite tool for creating search engine friendly URLs is Intelligencia's URLRewriter.dll. It is very easy to install in an ASP.Net website. After copying the Intelligencia.UrlRewriter.dll into the bin folder, just a few changes to the web.config file is needed. This tool came in very handy for our BestBrandsOn.TV website.

    In <configSections>,  add <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>.

    In <httpHandlers>, add <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>.

    Now all that is left is for the entries to handle the particular re-writes needed in the website. These go into a new section <rewriter></rewriter> within the <system.web> section.

    The BestBrandsOn.TV website has numerous categories of products such as Automative, Household, and Kitchen. I wanted to be able to list all products within a category using a URL of http://www.BestBrandsOn.TV/Automotive-Products.aspx or http://www.BestBrandsOn.TV/Household-Products.aspx. The general format of the URL is http://www.BestBrandsOn.TV/CategoryNameHere-Products.aspx. I added the following line in the <rewriter> section of web.config:

    <rewrite url="~/(.+)-Products.aspx" to="~/ProductList.aspx?Cat=$1" processing="stop"/>

    The URL of  http://www.BestBrandsOn.TV/CategoryNameHere-Products.aspx is rewritten to be http://www.BestBrandsOn.TV/ProductList.aspx?Cat=CategoryNameHere.

    I also wanted unique URLs for each of the products, again using words in the URL that made sense. For example, http://www.bestbrandson.tv/Household/Shamwow.aspx clearly shows the category name of Household and the product name of Shamwow. The rewrite needed for URLs of this nature required the following entry in the <rewriter> section:

    <rewrite url="~/Household/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>

    The general format of a URL like http://www.BestBrandsOn.TV/CategoryNameHere/ProductNameHere.aspx is rewritten to be http://www.BestBrandsOn.TV/Category/Product.aspx?Prod=ProductNameHere. I added a line in the web.config for each category.

    <rewrite url="~/Automotive/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
    <rewrite url="~/Electronics/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
    <rewrite url="~/Entertainment/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
    <rewrite url="~/Health-Beauty/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
    <rewrite url="~/Household/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
    <rewrite url="~/Kitchen/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
    <rewrite url="~/Fitness/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>

    You could make an even more generic rewriting rule. With this setup, I have to add a line in the web.config file every time we add a new category. I chose to do it this way, however, because we have quite a few real subdirectories and I didn't want to put in a lot of <unless> entries to exclude the real subdirectories for URL rewriting. Click here to see an example of a website with a more generalized entry where I did choose to use <unless> entries.

    Posted Dec 18 2008, 10:22 AM by Blogette with 1 comment(s)
    Filed under: ,
    Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web
  • How do the gas stations know?

    My gas tank in my van was getting close to empty. On my drive to work in the morning, I looked at each gas station along the way and took note of each one's price. In my 21 mile commute to work, I pass 14 gas stations. I planned on filling up at the cheapest place as I drove home from work that evening. Just a mere 9 hours later, as I started my commute to home, I discovered the every one of those gas stations had increased their prices during the day. Every one of them. The increase ranged from 3 cents to 11 cents more per gallon. How could every one of the raise their prices? Do they send out a memo to every gas station in town? Do they call each other? Do they raise their prices regularly or is it just before I need to fill my tank? They didn't even have the (bogus) excuse of an approaching hurricane.
    Posted Dec 16 2008, 09:20 AM by Blogette with 1 comment(s)
    Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web
  • Changing Skins Dynamically in ASP.Net using VB

    I thought I had a simple change in mind. On my website with web parts, I wanted my web part zones to have a dotted line around them when the user was in design mode and no border at all when the user was in browse mode. My page had three web part zones and I thought a dotted outline around each would let the user immediately know where a web part could be dragged. I created a WebPartZone.skin file in each of my four themes. The standard skin for a webpartzone had no border and a second skin called "Design" had a border. The code in the WebPartZone.skin file is as follows:

    <asp:WebPartZone runat="server">
        <PartChromeStyle BorderColor="#7A8FB8" BorderStyle="Solid" BorderWidth="2px" />
        <PartTitleStyle CssClass="titleBG" ForeColor="White" />
        <RestoreVerb ImageUrl="images/blue-Restore.GIF" />
        <CloseVerb ImageUrl="images/blue-Close.GIF" />
        <EditVerb ImageUrl="images/blue-Edit.GIF" />
        <MinimizeVerb ImageUrl="images/blue-Minimize.GIF" />
    </asp:WebPartZone>

    <asp:WebPartZone SkinID="Design" runat="server" BorderColor="Navy" BorderStyle="Dotted" BorderWidth="2px" DragHighlightColor="Navy">
        <PartChromeStyle BorderColor="#7A8FB8" BorderStyle="Solid" BorderWidth="2px" />
        <PartTitleStyle CssClass="titleBG" ForeColor="White" />
        <RestoreVerb ImageUrl="images/blue-Restore.GIF" />
        <CloseVerb ImageUrl="images/blue-Close.GIF" />
        <EditVerb ImageUrl="images/blue-Edit.GIF" />
        <MinimizeVerb ImageUrl="images/blue-Minimize.GIF" />
    </asp:WebPartZone>
     

    I had done some internet searching and I found many websites that said to change the skin dynamically, you need to assign the skin ID in the Page_PreInit event and each one had a sample shown. However, when I tried to do this, I received the very familiar error message "Object reference not set to an instance of an object.". I searched and searched and searched trying to find what I was doing wrong. Finally I found one website that explained that if you are using a master page, then the controls on your web page have not been generated yet in the Page_PreInit event. Who builds a website without a master page?!? I couldn't believe all of the other websites with samples hadn't mentioned this fact. The website that had a sample with a master page was written in C#. I was able to convert it to Visual Basic and add it to my website. I had to add one line of code in the Page_PreInit event to get the changing of the skin to work.

        Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
           Dim m As MasterPage = Master
          
    If Request("ctl00$cphFooter$ddlMode") = "Design" Then
              WebPartZone1.SkinID = "Design"
              WebPartZone2.SkinID = "Design"
              WebPartZone3.SkinID = "Design"
           Else    'use default skin
              WebPartZone1.SkinID = ""
              WebPartZone2.SkinID = ""
              WebPartZone3.SkinID = ""
           End
    If
        End Sub

    If my user had selected "Design" in a Mode drop down box, then I change the skin IDs in my three webpartzones to "Design", otherwise I set the skin ID back to the default by removing the value of skin ID.

    Posted Dec 15 2008, 03:11 PM by Blogette with no comments
    Filed under: ,
    Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web
  • Changing Themes Dynamically in ASP.Net using VB

    A working example of this demonstration can be viewed here: Changing Themes Dynamically. Most samples I found on the internet were written in C#, so I thought it might be helpful to have sample site written in Visual Basic. I have a simple page with a calendar control (also using the theme to determine its look) and a drop down box with the available themes to choose from. As a new theme is selected, the theme on the page is changed dynamically in the Page_PreInit sub and the choice is also saved in a cookie so that the theme is maintained when switching between web pages.

    For this demonstration, I made up four themes: blue, gray, green, and red. The images for each theme were named consistently, but including the theme name in each image file name so each name would be unique. This helps prevent cached images from being shown when the theme is changed dynamically. (All code and images can be downloaded here: ChangeThemes.zip.)

    To avoid having to put the same code on every page's Page_PreInit event, I created a new class that I called RDBasePage.vb which inherits from the System.Web.UI.Page class. I place the code to change the theme in the Page_PreInit on this class, and then on every web page I create I have it inherit from RDBasePage instead of System.Web.UI.Page.

     The steps taken in the code below are:

    1. Check for the selection made in the drop down box.
    2. If a selection has been made, save the selection in a cookie. If no selection was made, try to retrieve the selected theme from the cookie.
    3. If no selection was made or found in a cookie, default the theme to "gray".
    4. Assign the selected theme to the page.

    Imports System.Web.UI.Page
    Public Class RDBasePage
        Inherits System.Web.UI.Page
        Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            'check for new theme
            Dim selectedTheme As String = HttpContext.Current.Request("ctl00$ddlTheme")
           
    Dim objCookie As HttpCookie
           
    Try
                If selectedTheme > "-" Then    'save new selection
                    objCookie = New HttpCookie("RDTheme", selectedTheme)
                    HttpContext.Current.Response.Cookies.Add(objCookie)
               
    Else    'get theme from cookie
                    objCookie = HttpContext.Current.Request.Cookies("RDTheme")
                   
    If Not objCookie Is Nothing Then selectedTheme = objCookie.Value
                End If
            Catch
    ex
    As Exception
            Finally
                objCookie = Nothing
            End Try
            If selectedTheme <= "-" Then selectedTheme = "gray"
            Page.Theme = selectedTheme
        End Sub
    End Class


    The code needed on my web pages is very simple:

    Partial Class _Default
        Inherits RDBasePage

    End Class
     

    Posted Dec 10 2008, 03:07 PM by Blogette with no comments
    Filed under: ,
    Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web
  • Introduction to Lace Theory

    When I was about five or six years old, my friends and I went to pick up another friend at his house. He was still in his bedroom getting ready to go out, so we waited in the kitchen with his mother.  The boy called out a question to his mother that I couldn't quite make out. The mother then looked at each of our shoes and answered her son, "Liz is over, Tommy is under, and Sally is under." I had no idea what she was talking about and I must have looked at her quizzically because she explained what "over" and "under" meant. When lacing a shoe, you have the option to start the lacing by putting the laces down through the first set of holes which leaves the lace "over" the first set, or you can start the lacing by putting the laces up through the first set of holes which leaves the lace "under" the first set of holes. I was shocked! I had never thought about how my shoes were laced. I was also embarrassed; even though this friend was one or two years younger than I was, he knew about shoe lacing methods and I had been completely unaware. As soon as I got home, I determined how I would lace up my shoes, and I have laced my shoes that way ever since.

    Posted Dec 05 2008, 11:37 AM by Blogette with 2 comment(s)
    Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web
  • A Small Credit Card Victory

    I get paid twice a month and after my deposited paycheck clears, I always send my credit card company, Citibank, a check to pay the current balance off in full. Because of this practice, I rarely carry a balance and I check the charges as they appear in my account online every few days, so I don't pay much attention to my monthly statements. Last month I happened to have diverted all of my funds to pay off another debt, so I only sent Citibank one payment instead of two. Unfortunately, the payment I skipped was the one the was received before the "due date" on the statement. I was very surprised the next time I logged in to check my account and I discovered a $39 late fee. This was in addition to the finance charge that also appeared. I had no problem with the finance charge because I was late with a payment and I understand being penalized for it. However, the $39 late fee was just a bit too extreme for me to accept. I called Citibank's customer support line and told them I wanted to close my account. Of course they asked why I wanted to do that. I was very happy to explain to them that I had been a Citibank credit card holder for 20 years with my account always in good standing, and how offended I was that the one month I missed the due date I was hit with a $39 late fee. I could hear my voice shaking with anger and I knew they could hear that as well. The customer service representative reviewed my account, apologized that I was offended, explained that it wasn't personal, the computer adds the fee automatically, and said that she could remove the fee. And to sweeten the deal, she said she could lower my interest rate to 3.99% for 9 months if I agreed to remain a customer. Since I never carry a balance, the 3.99% interest rate didn't mean much to me, but I appreciated the extra effort. I was satisfied to have the late fee removed and I remain a Citibank customer.
    Posted Dec 01 2008, 09:09 AM by Blogette with 3 comment(s)
    Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web