Just another tech site

ASP.MVC Global.asax : Date validation


Definition:

The Global.asax file is an optional file used to declare and handle application and session-level events and objects for an ASP.NET web site running on an IIS Web Server. At run time, upon the arrival of the first request, Global.asax is parsed and compiled into a dynamically generated .NET Framework class.

Reference: http://en.wikipedia.org/wiki/Global.asax

ASP.MVC code sample

The following exemple will show how to route the user to a certain page depending of the current date.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    DateTime SurveyStart = DateTime.Now;
    DateTime SurveyStop = DateTime.Now;
    if (DateTime.TryParse(ConfigurationSettings.AppSettings.Get("DateSurveyStart"), out SurveyStart) &&
        DateTime.TryParse(ConfigurationSettings.AppSettings.Get("DateSurveyStop"), out SurveyStop))
    {
        if( SurveyStart <= DateTime.Now )         {             if (SurveyStop > DateTime.Now)
            {
                routes.MapRoute("brandId", "Survey/SubBrandList/{brandId}", new { controller = "Survey", action = "SubBrandList" });
                routes.MapRoute("search", "Survey/SubBrandListSearch/{search}", new { controller = "Survey", action = "SubBrandListSearch" });

                routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Survey", action = "Welcome", id = UrlParameter.Optional } // Parameter defaults
                );
            }
            else
            {
                // ENDED
                routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Survey", action = "ClosedSurvey", id = UrlParameter.Optional } // Parameter defaults
                );
            }
        }
        else
        {
            // COMMING SOON
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Survey", action = "ComingSoon", id = UrlParameter.Optional } // Parameter defaults
            );
        }
    }
    else
    {
        // ERROR COUNL NOT READ THE PARAMETERS
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Survey", action = "ComingSoon", id = UrlParameter.Optional } // Parameter defaults
        );
    }
}

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
}

The corresponding configuration file

<appSettings>
    <add key="DateSurveyStart" value="10/02/2011 14:57:32.8" />    
    <add key="DateSurveyStop" value="25/03/2011 14:57:32.8" />  
</appSettings>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: