Oct 15 2010

Adding and Deploying Generic Handlers (.ashx) to a SharePoint 2010 Visual Studio Project

Category: ASP.NET, SharePoint, TechnologyAdam Toth @ 2:00 pm

Generic Handlers (.ashx files) deployed to the _layouts directory are not directly supported by Visual Studio 2010 SharePoint projects like custom .aspx application pages are.

If you try to Add New Item… and select the Web or SharePoint categories in a VS 2010 SharePoint project, you won’t find Generic Handler anywhere.

image

You’ll find ASP.NET Handler, but this will require you to create entries in web.config to make your handler work. In order to add a new .ASHX generic handler and get it to deploy properly, you can use the following steps:

  • Right-click the project, and select Add New Item…
  • Choose the Application Page template.
  • In the name box, enter a name for your file, with an .ashx extension.
    image
  • Open the .ashx file, delete the contents and replace with the following, changing your Class= attribute with your desired namespace and class name:
    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

    <%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ WebHandler Language="C#" Class="MyNamespace.MyGenericHandler" %>

    Note: If you want to reference other SharePoint assemblies in your code-behind, you will need to add an @ Assembly directive for each DLL.

  • Open the ashx.cs file.
    • Add a using statement for System.Web.
    • You probably don’t need the using statement for Microsoft.SharePoint.WebControls, so remove it.
    • Change your namespace if necessary.
    • Change the class to inherit from IHttpHandler.
    • Implement the IHttpHandler interface. Your code should now look something like this:Â
      using System;

      using Microsoft.SharePoint;

      using System.Web;

       

      namespace MyNamespace

      {

          public partial class MyGenericHandler : IHttpHandler

          {

       

              #region IHttpHandler Members

       

              public bool IsReusable

              {

                  get { throw new NotImplementedException(); }

              }

       

              public void ProcessRequest(HttpContext context)

              {

                  throw new NotImplementedException();

              }

       

              #endregion

          }

      }

  • In the Solution Explorer, delete the ashx.designer.cs file, it is not needed.
  • In the Solution Explorer, click the .ashx file, and in the Properties pane, set the Build Action to Content.
  • In the Solution Explorer, click the .ashx.cs file, and in the Properties pane, set the Build Action to Compile.
  • Make sure to enable Token replacement for .ashx extensions. This will replace the $SharePoint.Project.AssemblyFullName$ token with the full strong name of your assembly, enabling you to reference other classes in your compiled assembly from the ashx code-behind.You can read more about token replacement here. To enable this for your Project, Unload your Project, Edit the .csproj file and add the following text to a PropertyGroup, and Reload your project:
    <PropertyGroup>

      <TokenReplacementFileExtensions>ashx</TokenReplacementFileExtensions>

    </PropertyGroup>

Tags: , , ,


Mar 06 2009

Helper Method for ASP.NET Wizard Controls

Category: ASP.NET, TechnologyAdam Toth @ 11:38 am

I enjoy working with the ASP.NET Wizard control – it’s one of the more useful controls available, and there always seems to be a place for a wizard in the custom apps I’ve written. Over time I’ve developed a helper method that makes it easier to do non-linear jumps from step to step.

GetIndexFromStep(WizardStepBase step)

Often, in a NextButtonClick event handler for a Wizard, you need to figure out what the current step is that you are on, based on the WizardNavigationEventArgs e.CurrentStepIndex property. Here is a helper method to enable you to do that:

/// <summary>
/// Returns the index of a particular wizard step in the WizardSteps collection
/// </summary>
/// <param name="step">The step whose index number you want</param>
/// <returns></returns>
private int GetIndexFromStep(WizardStepBase step)
{
    return step.Wizard.WizardSteps.IndexOf(step);
}

You can also package it up as an extension method (.NET 3.5) off of the Wizard or WizardStepBase object:

using System.Web.UI.WebControls;
 
/// <summary>
/// Extensions for ASP.NET Wizard Control Objects
/// </summary>
public static class WizardExtensions
{
    
    /// <summary>
    /// Gets the index of a particular wizard step.
    /// </summary>
    /// <param name="wizard">The current Wizard</param>
    /// <param name="step">The step whose index number you want</param>
    /// <returns></returns>
    public static int GetIndexFromStep(this Wizard wizard, WizardStepBase step)
    {
        return wizard.WizardSteps.IndexOf(step);
    }
    /// <summary>
    /// Gets the index of this wizard step.
    /// </summary>
    /// <param name="step">This step</param>
    /// <returns></returns>
    public static int GetIndex(this WizardStepBase step)
    {
        return step.Wizard.WizardSteps.IndexOf(step);
    }
}

If you’ve given your wizard steps specific IDs, then you can use them strongly typed in a NextButtonClick event:

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
    if (e.CurrentStepIndex == GetIndexFromStep(step1))
    {
        // We are on step #1
 
        // Validate...
 
        // Skip to step 3
        Wizard1.ActiveStepIndex = GetIndexFromStep(step3);
 
    }
}

Tags: ,