forked from poppastring/dasblog-core
/
SiteSearchBoxTagHelper.cs
80 lines (73 loc) · 2.25 KB
/
SiteSearchBoxTagHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using DasBlog.Core.Common;
using DasBlog.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Security.Policy;
using System.Threading.Tasks;
namespace DasBlog.Web.TagHelpers.Layout
{
public class SiteSearchBoxTagHelper : TagHelper
{
public string Id { get; set; } = "searchText";
public string Class { get; set; } = "col-md-3";
public string InnerClass { get; set; } = "pull-right affix";
public string ActionRoute { get; set; }
public string Action { get; set; }
public string Heading { get; set; } = "Search";
public string ButtonName { get; set; } = "Go";
public string ButtonClass { get; set; } = "btn";
private readonly IUrlHelper urlHelper;
private readonly IDasBlogSettings dasBlogSettings;
public SiteSearchBoxTagHelper(IDasBlogSettings dasBlogSettings, IHttpContextAccessor accessor)
{
this.dasBlogSettings = dasBlogSettings;
urlHelper = accessor.HttpContext?.Items[typeof(IUrlHelper)] as IUrlHelper;
if (urlHelper == null)
{
throw new Exception("No UrlHelper found in http context");
}
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
string actionUrl;
if (ActionRoute != null)
{
actionUrl = urlHelper.RouteUrl(ActionRoute);
}
else if (Action != null)
{
actionUrl = Action;
}
else
{
var baseUri = new Uri(dasBlogSettings.SiteConfiguration.Root);
var myUri = new Uri(baseUri, urlHelper.RouteUrl(Constants.SearcherRouteName));
actionUrl = myUri.AbsoluteUri;
}
output.TagName = string.Empty;
output.Content.Clear();
output.Content.AppendHtml($@"
<div class='{Class}'>
<div class='{InnerClass}'>
<div class='card'>
<div class='card-header'>{Heading}</div>
<div class='card-body'>
<form method='post' action='{actionUrl}'>
<input type='text' name='searchText' id='{Id}'/>
<input class='{ButtonClass}' type='submit' value='{ButtonName}'/>
</form>
</div>
</div>
</div>
</div>
</div>
");
}
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
return Task.Run(() => Process(context, output));
}
}
}