Archive for category Code Snippet

Using ActionFilters for Logging in ASP.Net MVC

The ActionFilterAttribute is a great way to create a global logging procedure for entry and exit of an ActionResult method in your MVC Controllers.

Create a class that inherits from ActionFilterAttribute and implements IActionFilter.

    [AttributeUsage(AttributeTargets.Method)]
    public sealed class LogRequestAttribute : ActionFilterAttribute, IActionFilter
    {
        ///
        /// Log exit of the ActionResult method
        ///
        ///
        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
        {
            // Put code you want to log when the ActionResult has completed in here.
        }

        ///
        /// Log entry into the ActionResult method
        ///
        ///
        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Put code you want to log when the ActionResult is entered in here.
        }
    }

To implement in your code, all you do is add the attribute to the ActionResult method:

        [LogRequestAttribute]
        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

When the ActionResult executes, it will hit in this order:

  1. OnActionExecuting
  2. OnActionExecuted

Using the ActionFilterAttribute saves you from having to write the same code over and over for every method in your Controller.

Here’s an example of using the ActionFilterAttribute for rolling your own security from Adrew Siemer’s blog:

http://geekswithblogs.net/AndrewSiemer/archive/2008/11/17/mvc-attributes-via-actionfilterattribute-again.aspx

Tags: ,

Save/Restore Winforms Position on Start/Shutdown

Saving the form’s position and restoring it on startup is a simple process when using the Settings functionality that’s saved with each user of your application.

Create a few settings in your Settings tab for the project and set them as User scope. Use the below picture as your guide:

Settings

Settings

In the form you want to save add the following text to your FormClosing, and Load events:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.WindowState != FormWindowState.Minimized)
    {
        Properties.Settings.Default.MainForm_WindowState = this.WindowState;
        Properties.Settings.Default.MainForm_Location = this.Location;
        Properties.Settings.Default.MainForm_Size = this.Size;
    }
    Properties.Settings.Default.Save();
}

private void MainForm_Load(object sender, EventArgs e)
{
    if (Properties.Settings.Default.MainForm_WindowState == FormWindowState.Maximized)
    {
        this.WindowState = FormWindowState.Maximized;
    }
    else
    {
        this.WindowState = Properties.Settings.Default.MainForm_WindowState;
        this.Location = Properties.Settings.Default.MainForm_Location;
        if (Properties.Settings.Default.MainForm_Size.IsEmpty)
        {
            this.Width = Screen.GetWorkingArea(this).Width - 200;
            this.Height = Screen.GetWorkingArea(this).Height - 100;
        }
        else
       {
           this.Size = Properties.Settings.Default.MainForm_Size;
       }
       if (this.Top < 0)
           this.Top = 0;
   }
}

You'll notice that we're checking to only save when not minimized. We're also ensuring that the form is within view and is not somehow off the sceeen.

Tags:

Subscribe in a reader

Subscribe via Email RSS Feed:

Delivered by FeedBurner