Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads
 
Add your own
alternative version

Building a Web Message Board using Visual Studio 2008, Part I - The Basic Message Board

, 30 Dec 2007 CPOL
This article builds a web based message board and uses several new technologies introduced with Visual Studio 2008 such as LINQ, WCF Web Programming, WCF Syndication, ASP.NET ListView, ASP.NET DataPager etc.
MessageBoard.zip
MessageBoard
MessageBoard.Core
MessageBoard.snk
Properties
MessageBoard.DataAccess.Linq
MessageBoard.snk
Properties
Settings.settings
MessageBoard.DataAccess.NonLinq
MessageBoard.snk
Properties
MessageBoard.Web
MessageBoard.snk
Properties
Service References
MessageBoard
App_Code
App_Data
App_Themes
Default
Floating
MessageBoard.png
newpost.jpg
post.jpg
Outlook
controlBarGradient.jpg
headerGradient.jpg
headerGradient2.jpg
ClassDiagram1.cd
Feed.svc
Global.asax
images
rss16.png
Install
Application Files
MessageBoard.Word_1_0_0_0
MessageBoard.dotx
MessageBoard.Word.dll.config.deploy
MessageBoard.Word.dll.deploy
MessageBoard.Word.dll.manifest
MessageBoard.Word.vsto
MessageBoard.dotx
MessageBoard.Word.vsto
setup.exe
MessageBoardService.svc
WebSite
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MessageBoard.Web.Properties;
using System.Runtime.Serialization;

namespace MessageBoard.Web
{
    /// <summary>
    /// This class has all teh helper functions to detect and save time zone values
    /// </summary>
    public static class TimeZoneUtility
    {
        /// <summary>
        /// Gets a list of time zones which have the specified UTC offset in minutes. If the string is invalid
        /// all time zones are returned.
        /// </summary>
        /// <param name="jsUtcOffset">The Utc offset from JavaScript ontained from the Request</param>
        /// <returns>List of time zones</returns>
        public static IEnumerable<TimeZoneInfo> GetTimeZonesFromUtcOffset(string jsUtcOffset)
        {
            int timeZoneOffset = 0;

            return (Int32.TryParse(jsUtcOffset, out timeZoneOffset)) ?
                GetTimeZonesFromUtcOffset(timeZoneOffset) :
                TimeZoneInfo.GetSystemTimeZones();
        }

        /// <summary>
        /// Returns timezone offset from specified offset obtained from JavaScript
        /// </summary>
        /// <param name="timeZoneOffset">Offset obtained from javaScript's getTimezoneOffset method</param>
        /// <returns>List of time zones</returns>
        public static IEnumerable<TimeZoneInfo> GetTimeZonesFromUtcOffset(int timeZoneOffset)
        {
            TimeSpan jsOffsetTimeSpan = new TimeSpan(0, -timeZoneOffset, 0);

            //UTC offset is in range from -14 to +14 hours
            //TimeSpan.Duration gives us the absolute value
            if (jsOffsetTimeSpan.Duration().TotalHours > 14)
                return TimeZoneInfo.GetSystemTimeZones();

            return from tz in TimeZoneInfo.GetSystemTimeZones()
                   where tz.BaseUtcOffset.Equals(jsOffsetTimeSpan)
                   select tz;
        }
    
        /// <summary>
        /// Returns TimeZoneInfo given the time zone id. If the timezone is invalid
        /// it returns the local timzezone
        /// </summary>
        /// <param name="id">The timezone id specified as string</param>
        /// <returns>The time zone info</returns>
        public static TimeZoneInfo GetTimeZoneFromId(string id)
        {
            if (String.IsNullOrEmpty(id))
                return TimeZoneInfo.Local;

            TimeZoneInfo info;

            try
            {
                info = TimeZoneInfo.FindSystemTimeZoneById(id);
            }
            catch (TimeZoneNotFoundException)
            {
                info = TimeZoneInfo.Local;
            }

            return info;
        }

        private static readonly string CookieName = "__TimeZoneInfo";

        /// <summary>
        /// Saves the time zone info in a cookie
        /// </summary>
        /// <param name="info">The time zone info to save in a cookie</param>
        public static void SaveTimeZoneInfoInCookie(TimeZoneInfo info)
        {
            HttpContext context = HttpContext.Current;

            if (context == null)
                throw new InvalidOperationException(Resources.NullHttpContext);
            
            HttpCookie cookie = new HttpCookie(CookieName, info.Id);
            cookie.Expires = DateTime.Now.AddYears(1); //Expire after a year
            context.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// Uses the current HttpContext to obtain a TimzeZoneInfo value stored in a cookie.
        /// If no such TimezonInfo is found or the cookie data is invalid this function returns null
        /// </summary>
        /// <returns>The TimeZoneInfo from the cookie or null otherwise</returns>
        public static TimeZoneInfo GetTimeZoneInfoFromCookie()
        {
            HttpContext context = HttpContext.Current;

            if (context == null)
                throw new InvalidOperationException(Resources.NullHttpContext);

            HttpCookie cookie = context.Request.Cookies[CookieName];

            TimeZoneInfo info = TimeZoneInfo.Utc;

            if (cookie == null || String.IsNullOrEmpty(cookie.Value))
                return info;

            try
            {
                info = TimeZoneInfo.FindSystemTimeZoneById(cookie.Value);

            }
            catch (TimeZoneNotFoundException )
            {
                //It's ok just return null               
            }

            return info;
        }

        public static DateTime ConvertToCurrentTimeZone(DateTime dateTime)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneUtility.GetRequestTimeZone());
        }

        private static object _timeZoneKey = new object();

        /// <summary>
        /// Gets the current time zone of the Request
        /// </summary>
        /// <returns>The timezone value</returns>
        public static TimeZoneInfo GetRequestTimeZone()
        {
            TimeZoneInfo info = HttpContext.Current.Items[_timeZoneKey] as TimeZoneInfo;

            if (info == null)
            {
                info = GetTimeZoneInfoFromCookie();
                HttpContext.Current.Items[_timeZoneKey] = info;
            }

            return info;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rama Krishna Vavilala
Architect
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 30 Dec 2007
Article Copyright 2007 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid