Palermo4 on MSDN

Technical Blog of J. Michael Palermo IV

Check it out!

Posts
  • Palermo4 on MSDN

    AJAX Extension Methods

    • 0 Comments

    Although the ScriptManager has a property named IsInAsyncPostBack to determine an AJAX call, the control is scoped to the page it is contained in.  If writing code in a custom HTTP module, what if I need to know in the BeginRequest event whether the current request is AJAX or JSON?

    How nice it would be to simply ask the request if it is in the context of an AJAX or JSON call.  In Visual Studio 2008/2010, I can do just that.  With extension methods, I can add two new behaviors to the HttpRequest object.  Here are my desired methods:

    namespace Palermo4.Web.Extensions
    {
        public static class WebExtensions
        {
            public static bool IsJson(this HttpRequest request)
            {
                return request.ContentType.StartsWith("application/json",
                    StringComparison.OrdinalIgnoreCase);
            }
    
            public static bool IsAjax(this HttpRequest request)
            {
                return (request.Headers.Get("x-microsoftajax") ?? "")
                    .Equals("delta=true", StringComparison.OrdinalIgnoreCase);
            }        
        }
    }

    The above methods simply return whether the content type of the request matches application/json (JSON call) or x-microsoftajax (AJAX call).

    To make use of my new methods in the global.asax, I need to add using statement for the namespace the extension methods are contained in.

    using Palermo4.Web.Extensions;
    
    public class Global : System.Web.HttpApplication
    {
        protected void Application_BeginRequest(object sender, EventArgs e)
        {        
            if (!Request.IsAjax() && !Request.IsJson())
            {
                // this is not an AJAX or JSON request
            }
        }
    }

    I can also make use of this namespace in my web.config file as follows:

    <configuration>
      <system.web>
        <pages>
          <namespaces>
            <add namespace="Palermo4.Web.Extensions" />
          </namespaces>
        </pages>
      </system.web>
    </configuration>

    Now I can use my methods in any page as seen below:

    page_request_isajax_isjson

  • Palermo4 on MSDN

    So...

    • 3 Comments

    In one of my earliest blog posts ever, I made an observation about how Microsoft employees use the word 'so'.  So in honor of that blog post, I plan on acclimating to the machine with my own overuse of the word.

    I am so thrilled to announce that I am now employed by Microsoft as a Developer Evangelist.  My geographic area of coverage includes Arizona, Colorado, New Mexico, Utah, and southern Nevada.  So that means I get to work with great community leaders.  It also means I get to work with awesome people inside Microsoft.  I look forward to meeting so many more people as I grow into my role.

    So why this blog?  I have a blog at http://weblogs.asp.net/palermo4/, and I blog at http://www.palermo4.com.  So why another blog location?  This blog will be my new home for technical blogs.  The other blog sites will continue to function for non-technical content.  So with the exception of this post, all forthcoming blog posts will be technical in nature and likely contain much code.

    So, any comments?

     

  • Palermo4 on MSDN

    CNN Report: Arizona Desert Getting Cooler? [del.icio.us]

    ...read more
  • Palermo4 on MSDN

    VB.NET and C# Comparison [del.icio.us]

    ...read more
  • Palermo4 on MSDN

    Animoto - the end of slideshows [del.icio.us]

    ...read more
  • Palermo4 on MSDN

    Geosense for Windows [del.icio.us]

    Geosense is a Windows Sensor that provides the Location and Sensors platform in Windows 7 with accurate and reasonably ubiquitous positioning information without requiring or the assistance of GPS hardware, enabling more practical location-based applications...
  • Palermo4 on MSDN

    Web Application Projects using ProfileProviders [del.icio.us]

    ...read more
  • Palermo4 on MSDN

    SelectORacle [del.icio.us]

    This site gives simple ...read more
  • Palermo4 on MSDN

    ASP.NET Web Forms MVP [del.icio.us]

    ...read more
  • Palermo4 on MSDN

    ASP.NET resources [del.icio.us]

    ...read more
Page 17 of 18 (176 items) «1415161718