پیاده سازی Open Search در ASP.NET MVC
نویسنده: وحید نصیری
تاریخ: ۱۳۹۲/۰۴/۱۰ ۸:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<?xml version="1.0" encoding="UTF-8" ? /> <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"> <ShortName>My Site's Asset Finder</ShortName> <Description>Find all your assets</Description> <Url type="text/html" method="get" template="http://MySite.com/Home/Search/?q=searchTerms"/> <InputEncoding>UTF-8</InputEncoding> <SearchForm>http://MySite.com/</SearchForm> </OpenSearchDescription>
using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
namespace WebToolkit
{
public class OpenSearchResult : ActionResult
{
public string ShortName { set; get; }
public string Description { set; get; }
public string SearchForm { set; get; }
public string FavIconUrl { set; get; }
public string SearchUrlTemplate { set; get; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var response = context.HttpContext.Response;
writeToResponse(response);
}
private void writeToResponse(HttpResponseBase response)
{
response.ContentEncoding = Encoding.UTF8;
response.ContentType = "application/opensearchdescription+xml";
using (var xmlWriter = XmlWriter.Create(response.Output, new XmlWriterSettings { Indent = true }))
{
xmlWriter.WriteStartElement("OpenSearchDescription", "http://a9.com/-/spec/opensearch/1.1/");
xmlWriter.WriteElementString("ShortName", ShortName);
xmlWriter.WriteElementString("Description", Description);
xmlWriter.WriteElementString("InputEncoding", "UTF-8");
xmlWriter.WriteElementString("SearchForm", SearchForm);
xmlWriter.WriteStartElement("Url");
xmlWriter.WriteAttributeString("type", "text/html");
xmlWriter.WriteAttributeString("template", SearchUrlTemplate);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Image");
xmlWriter.WriteAttributeString("width", "16");
xmlWriter.WriteAttributeString("height", "16");
xmlWriter.WriteString(FavIconUrl);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Close();
}
}
}
}using System.Web.Mvc;
namespace Readers
{
public partial class OpenSearchController : Controller
{
public virtual ActionResult Index()
{
var fullBaseUrl = Url.Action(result: MVC.Home.Index(), protocol: "http");
return new OpenSearchResult
{
ShortName = ".NET Tips",
Description = ".NET Tips Contents Search",
SearchForm = fullBaseUrl,
FavIconUrl = fullBaseUrl + "favicon.ico",
SearchUrlTemplate = Url.Action(result: MVC.Search.Index(), protocol: "http") + "?term={searchTerms}"
};
}
}
}<link href="@Url.Action(result: MVC.OpenSearch.Index(), protocol: "http")" rel="search"
title=".NET Tips Search" type="application/opensearchdescription+xml" />PM> Install-Package DNTCommon.Web.Core
پس از وارد کردن آدرس سایت، با فشردن دکمهی tab و یا حتی کلیک بر روی دکمهی tab ای که در تصویر مشخص شده، امکان جستجو را در سایت فراهم میکند: