Ahmad Masykur

Share your knowledge although one function!

About the author

Ahmad Masykur is a Software Architecture Engineer at PT. Freeport Indonesia Jakarta Indonesia.
In this blog, I share things of interest to me. Most topics are likely to be related to software development, but don't hold me to it.

Certificates



Awards


Powered by

Widget Prayer Time not found.

There is an error in XML document (4, 16278).X

Page List

Validators


Ahmad Masykur

Create IE8 Accelerator for BlogEngine.NET 1.5

I have created IE8 WebSlices for BlogEngine.NET 1.5 yesterday. I will add new feature stuff to BlogEngine to support IE8 features. Today, I have wrote new IE8 accelerator for BlogEngine.NET 1.5. I’m added new handler named AcceleratorHandler to BlogEngine to handle accelerator request. This handler provide accelerator XML information and preview page.

I have add new menu named Accelerator in my BlogEngine theme.

image

Click Accelerator menu to add the accelerator and then click Add button on the confirmation dialog window.

image

If we navigate away from our site and want to search text from our site. Select text inside the page and navigate to the accelerator. Now you can search from your site anywhere. Here is screen capture of the accelerator.

image

To enable this feature to BlogEngine.NET. Please follow steps below.

Create AcceleratorHandler

Create new handler for accelerator in BlogEngine.Core project. Here is the source code of the accelerator.

using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.IO;
using System.Web.UI;
namespace BlogEngine.Core.Web.HttpHandlers
{
    class AcceleratorHandler : IHttpHandler
    {
        #region IHttpHandler Members
        public bool IsReusable
        {
            get { return false; }
        }
        public void ProcessRequest(HttpContext context)
        {
            if (!string.IsNullOrEmpty(context.Request.QueryString["q"]))
            {
                // Searches posts and pages
                WriteSearchResult(context);
            }
            else
            {
                WriteAcceleratorInfo(context);
            }
        }
        private void WriteSearchResult(HttpContext context)
        {
            List<IPublishable> searchResult = Search.Hits(context.Request.QueryString["q"], false);
            context.Response.ContentType = "text/html";
            context.Response.AppendHeader("Content-Disposition", "inline; filename=search.html");
            Stream  stream = context.Response.OutputStream;
            using (TextWriter streamWriter = new StreamWriter(stream))
            {
                using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(streamWriter))
                {
                    htmlTextWriter.WriteFullBeginTag("html");
                    htmlTextWriter.WriteFullBeginTag("head");
                    htmlTextWriter.WriteFullBeginTag("title");
                    htmlTextWriter.Write(BlogSettings.Instance.Name);
                    htmlTextWriter.WriteEndTag("title");
                    htmlTextWriter.WriteEndTag("head");
                    htmlTextWriter.WriteBeginTag("body");
                    htmlTextWriter.WriteAttribute("style", "font-family:Arial,Hevetica,Sans-serif;");
                    htmlTextWriter.Write(HtmlTextWriter.TagRightChar);
                    htmlTextWriter.WriteFullBeginTag("dl");
                    foreach (IPublishable result in searchResult)
                    {
                        htmlTextWriter.WriteFullBeginTag("dt");
                        htmlTextWriter.WriteBeginTag("a");
                        htmlTextWriter.WriteAttribute("href", result.AbsoluteLink.ToString());
                        htmlTextWriter.Write(HtmlTextWriter.TagRightChar);
                        htmlTextWriter.Write(result.Title);
                        htmlTextWriter.WriteEndTag("a");
                        htmlTextWriter.WriteBeginTag("br");
                        htmlTextWriter.Write(HtmlTextWriter.SelfClosingTagEnd);
                        htmlTextWriter.WriteBeginTag("dd");
                        htmlTextWriter.WriteAttribute("style", "margin:0px;font-size:smaller;");
                        htmlTextWriter.Write(HtmlTextWriter.TagRightChar);
                        string description;
                        if (string.IsNullOrEmpty(result.Description))
                        {
                            string desc = Utils.StripHtml(result.Content);
                            string shortDesc;
                            if (desc.Length < 200)
                            {
                                shortDesc = desc;
                            }else
                            {
                                shortDesc = desc.Substring(0, 196) + " ...";
                            }
                            description = shortDesc;
                        } else
                        {
                            description = result.Description;
                        }
                        htmlTextWriter.Write(description);
                        htmlTextWriter.WriteEndTag("dd");
                        htmlTextWriter.WriteEndTag("dt");
                    }
                    htmlTextWriter.WriteEndTag("dl");
                    htmlTextWriter.WriteEndTag("body");
                    htmlTextWriter.WriteEndTag("html");
                }
            }
            context.Response.Flush();
            context.Response.End();
        }
        private void WriteAcceleratorInfo(HttpContext context)
        {
            context.Response.ContentType = "application/xml";
            context.Response.AppendHeader("Content-Disposition", "inline; filename=accelerator.xml");
            XmlWriterSettings writerSettings = new XmlWriterSettings();
            writerSettings.Encoding = System.Text.Encoding.UTF8;
            writerSettings.Indent = true;
            Stream stream = context.Response.OutputStream;
            using (XmlWriter writer = XmlWriter.Create(stream, writerSettings))
            {
                writer.WriteStartElement("openServiceDescription", "http://www.microsoft.com/schemas/openservicedescription/1.0");
                writer.WriteElementString("homepageUrl", Utils.AbsoluteWebRoot.ToString());
                writer.WriteStartElement("display");
                writer.WriteElementString("name", "Search in " + BlogSettings.Instance.Name + " weblog");
                writer.WriteElementString("icon", new Uri(Utils.AbsoluteWebRoot, "pics/blogengine.ico").ToString());
                writer.WriteEndElement();
                writer.WriteStartElement("activity");
                writer.WriteAttributeString("category", "search");
                writer.WriteStartElement("activityAction");
                writer.WriteAttributeString("context", "selection");
                writer.WriteStartElement("preview");
                writer.WriteAttributeString("action", new Uri(Utils.AbsoluteWebRoot, "accelerator.axd").ToString());
                writer.WriteStartElement("parameter");
                writer.WriteAttributeString("name", "q");
                writer.WriteAttributeString("value", "{selection}");
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteStartElement("execute");
                writer.WriteAttributeString("action", new Uri(Utils.AbsoluteWebRoot, "search.aspx").ToString());
                writer.WriteStartElement("parameter");
                writer.WriteAttributeString("name", "q");
                writer.WriteAttributeString("value", "{selection}");
                writer.WriteAttributeString("type", "text");
                writer.WriteFullEndElement();
            }
            context.Response.Flush();
            context.Response.End();
        }
        #endregion
    }
}

 

Add handler to web.config file.

I the httlHandlers, add following line

<httpHandlers>
    <add verb="*"
         path="accelerator.axd"
         type="BlogEngine.Core.Web.HttpHandlers.AcceleratorHandler, BlogEngine.Core"
         validate="false"/>

In the handlers element, add following line

<handlers accessPolicy="Read, Write, Script, Execute">
    <add name="AcceleratorHandler" 
         verb="*" 
         path="accelerator.axd" 
         type="BlogEngine.Core.Web.HttpHandlers.AcceleratorHandler, BlogEngine.Core" 
         resourceType="Unspecified" 
         requireAccess="Script" 
         preCondition="integratedMode"/>

Add new button to your selected theme. Your modification may look like lines below.

<a href="javascript:window.external.AddService('<%=Utils.AbsoluteWebRoot + "accelerator.axd" %>');" style="float:left">Accelerator</a>

Rebuild you solution and republish to hosted server.

Enjoy with new stuff of IE8 accelerator.


Categories: BlogEngine
Permalink | Comments (0) | Post RSSRSS comment feed

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading