ASP.NET MVC 4 ile mvc.sitemap dosyası içinde LINQ sorgusu ile arama yapma
Bazen yaptığımız web uygulamaları büyür büyür, dallanıp budaklanıp aradığınızı zor bulmanıza sebep olabilir. Böyle durumlarda sitenizde mini bir arama motoru olmasını isteyebilirsiniz. ASP.NET MVC 4 ile bunun nasıl yapılabileceğini göstermek istiyorum. Öncelikle uygulamanızda “MvcSiteProvider” nuGet paketini ekliyoruz.
Arama Sonuçlarını Göstermek İhtiyacımız olan ViewModel :
public class SearchResults
{
public string Title { get; set; }
public string ControllerName { get; set; }
public string Description { get; set; }
}
HomeController’ımıza Arama Yapacak Action’ı ekliyoruz:
public ActionResult SearchResults(string searchString)
{
if (!String.IsNullOrEmpty(searchString))
{
ViewBag.CurrentFilter = searchString;
SiteMapPathHelperModel mdl = new SiteMapPathHelperModel();
SiteMapNode startingNode = SiteMap.Provider.RootNode;
for (SiteMapNode node = startingNode; node != null; node = node.ParentNode
{
MvcSiteMapProvider.MvcSiteMapNode mvcNode = node as MvcSiteMapProvider.MvcSiteMapNode;
if (mvcNode != null)
foreach (var cnode in mvcNode.GetAllNodes())
{
mdl.Nodes.Add(new SiteMapNodeModel{
Title = ((MvcSiteMapProvider.MvcSiteMapNode) cnode).Title,
Controller = ((MvcSiteMapProvider.MvcSiteMapNode) cnode).Controller,
Description = ((MvcSiteMapProvider.MvcSiteMapNode) cnode).Description,
Action = ((MvcSiteMapProvider.MvcSiteMapNode) cnode).Action
});
}
}
IEnumerable<searchresults> result = (from page in mdl.Nodes where (page.Controller.ToUpper().Contains(searchString.ToUpper())
|| page.Title.ToUpper().Contains(searchString.ToUpper())
|| page.Description.ToUpper().Contains(searchString.ToUpper()))
&& page.Action == "Index"
select new SearchResults
{
Title = page.Title,
ControllerName = page.Controller,
Description = page.Description
});
return View(result);
}
return View();
}
Kodumuz mvc.sitemap dosyamızda Controller, Title ve Description alanlarında arama bilgimizin olup olmadığını kontrol ediyor.
View oluşturduğumuzda şöyle bir kod basitçe işimizi görecektir.
@model IEnumerable<viewmodels.searchresults>
@{
ViewBag.Title = “SearchResults”;
}
<h2>Search Results</h2>
<style>
.highlight {
font-weight: bold;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
$(".portfolio-block").highlight("@ViewBag.CurrentFilter");
});
</script>
<div class="row-fluid search-forms search-default">
<form class="form-search" method="get">
<div class="chat-form">
<div class="input-cont">
<input name="searchString" type="text" value="@ViewBag.CurrentFilter" placeholder="Search…/" />
</div>
<button class="btn green" type="submit">
Search <i class="m-icon-swapright m-icon-white"></i></button>
</div>
</form>
@if(Model!=null)
{
foreach(var result in Model)
{
<div class="row-fluid portfolio-block">
<h4>@Html.ActionLink(result.Title, "Index", result.ControllerName)</h4>
<a href="/@result.ControllerName/Index">/@result.ControllerName</a>
<p>@result.Description</p>
}
}
else
{
<div class="row-fluid portfolio-block">
No results found.
</div>
}
Eğer kodumuza JQuery Highlight plugin i yüklemişsek arama sonuçlarında arama kelimelerini belirginleştirebiliriz.
Örnek mvc.sitemap dosyası:
<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0"
xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0 MvcSiteMapSchema.xsd" enableLocalization="true">
<mvcSiteMapNode title="Users" controller="User" action="Index" description="Managament page of users and their role.">
<mvcSiteMapNode title="Create User" controller="User" action="Create" />
<mvcSiteMapNode title="Edit User" controller="User" action="Edit" />
<mvcSiteMapNode title="Delete User" controller="User" action="Delete" />
<mvcSiteMapNode title="User Details" controller="User" action="Details" />
<mvcSiteMapNode title="User Roles" controller="Role" action="Index" description="Define role names for site usage.">
<mvcSiteMapNode title="Create Role" controller="Role" action="Create" />
<mvcSiteMapNode title="Edit User Role" controller=“Role" action=“Edit" />
<mvcSiteMapNode title="Delete User Role" controller=“Role" action=“Delete" />
<mvcSiteMapNode title="User Role Details" controller=“Role" action=“Details" />
Arama sorgumuz basitçe mvc.sitemap dosyasındaki bütün nodları tarar ancak ben örnekte sadece Action=”Index” olarak filtreledim. Siz LINQ sorgusunu kullanarak daha da geliştirebilirsiniz.