|
| 1 | +using System.Collections.Generic; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +public static class HtmlHelper |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Phát sinh trang HTML |
| 9 | + /// </summary> |
| 10 | + /// <param name="title">Tiêu đề trang</param> |
| 11 | + /// <param name="content">Nội dung trong thẻ body</param> |
| 12 | + /// <returns>Trang HTML</returns> |
| 13 | + public static string HtmlDocument(string title, string content) { |
| 14 | + return $@" |
| 15 | + <!DOCTYPE html> |
| 16 | + <html> |
| 17 | + <head> |
| 18 | + <meta charset=""UTF-8""> |
| 19 | + <title>{title}</title> |
| 20 | + <link rel=""stylesheet"" href=""/css/site.min.css"" /> |
| 21 | + <script src=""/js/jquery.min.js""> |
| 22 | + </script><script src=""/js/popper.min.js""> |
| 23 | + </script><script src=""/js/bootstrap.min.js""></script> |
| 24 | + </head> |
| 25 | + <body> |
| 26 | + {content} |
| 27 | + </body> |
| 28 | + </html>"; |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Phát sinh HTML thanh menu trên, menu nào active phụ thuộc vào URL mà request gủi đến |
| 34 | + /// </summary> |
| 35 | + /// <param name="menus">Mảng các menu item, mỗi item có cấu trúc {url, lable}</param> |
| 36 | + /// <param name="request">HttpRequest</param> |
| 37 | + /// <returns></returns> |
| 38 | + |
| 39 | + public static string MenuTop(object[] menus, HttpRequest request) { |
| 40 | + |
| 41 | + var menubuilder = new StringBuilder(); |
| 42 | + menubuilder.Append("<ul class=\"navbar-nav\">"); |
| 43 | + foreach (dynamic menu in menus) { |
| 44 | + string _class = "nav-item"; |
| 45 | + // Active khi request.PathBase giống url của menu |
| 46 | + if (request.PathBase == menu.url) _class += " active"; |
| 47 | + menubuilder.Append($@" |
| 48 | + <li class=""{_class}""> |
| 49 | + <a class=""nav-link"" href=""{menu.url}"">{menu.label}</a> |
| 50 | + </li> |
| 51 | + "); |
| 52 | + } |
| 53 | + menubuilder.Append("</ul>\n"); |
| 54 | + |
| 55 | + string menuhtml = $@" |
| 56 | + <div class=""container""> |
| 57 | + <nav class=""navbar navbar-expand-lg navbar-dark mainbackground""> |
| 58 | + <a class=""navbar-brand"" href=""/"">XTLAB</a> |
| 59 | + <button class=""navbar-toggler"" type=""button"" |
| 60 | + data-toggle=""collapse"" data-target=""#my-nav-bar"" |
| 61 | + aria-controls=""my-nav-bar"" aria-expanded=""false"" aria-label=""Toggle navigation""> |
| 62 | + <span class=""navbar-toggler-icon""></span> |
| 63 | + </button> |
| 64 | + <div class=""collapse navbar-collapse"" id=""my-nav-bar""> |
| 65 | + {menubuilder} |
| 66 | + </div> |
| 67 | + </nav></div>"; |
| 68 | + |
| 69 | + return menuhtml; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Những menu item mặc định cho trang |
| 74 | + /// </summary> |
| 75 | + /// <returns>Mảng các menuitem</returns> |
| 76 | + public static object[] DefaultMenuTopItems() { |
| 77 | + return new object[] { |
| 78 | + new { |
| 79 | + url = "/RequestInfo", |
| 80 | + label = "Request" |
| 81 | + }, |
| 82 | + new { |
| 83 | + url = "/Product", |
| 84 | + label = "Product" |
| 85 | + } |
| 86 | + , |
| 87 | + new { |
| 88 | + url = "/Encoding", |
| 89 | + label = "Encoding" |
| 90 | + }, |
| 91 | + new { |
| 92 | + url = "/Cookies", |
| 93 | + label = "Cookies" |
| 94 | + }, |
| 95 | + new { |
| 96 | + url = "/Json", |
| 97 | + label = "JSON" |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + public static string HtmlTrangchu() { |
| 103 | + return $@" |
| 104 | + <div class=""container""> |
| 105 | + <div class=""jumbotron""> |
| 106 | + <h1 class=""display-4"">Đây là một trang Web .NET Core</h1> |
| 107 | + <p class=""lead"">Trang Web này xây dựng trên nền tảng <code>.NET Core</code>, |
| 108 | + chưa sử dụng kỹ thuật MVC - nhằm mục đích học tập. |
| 109 | + Mã nguồn trang này tại <a target=""_blank"" |
| 110 | + href=""https://github.com/xuanthulabnet/learn-cs-netcore/blob/master/ASP_NET_CORE/03.RequestResponse/""> |
| 111 | + Mã nguồn Ví dụ</a> |
| 112 | + |
| 113 | + </p> |
| 114 | + <hr class=""my-4""> |
| 115 | + <p><code>.NET Core</code> là một hệ thống chạy đa nền tảng (Windows, Linux, macOS)</p> |
| 116 | + <a class=""btn btn-danger btn-lg"" href=""https://xuanthulab.net/lap-trinh-c-co-ban/"" role=""button"">Xem thêm</a> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + "; |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + // Mở rộng String, phát sinh thẻ HTML với nội dụng là String |
| 124 | + // Ví dụ: |
| 125 | + // "content".HtmlTag() => <p>content</p> |
| 126 | + // "content".HtmlTag("div", "text-danger") => <div class="text-danger">content</div> |
| 127 | + public static string HtmlTag(this string content, string tag = "p", string _class = null) { |
| 128 | + string cls = (_class != null) ? $" class=\"{_class}\"":null; |
| 129 | + return $"<{tag + cls}>{content}</{tag}>"; |
| 130 | + } |
| 131 | + public static string td(this string content, string _class = null) { |
| 132 | + return content.HtmlTag("td", _class); |
| 133 | + } |
| 134 | + public static string tr(this string content, string _class = null) { |
| 135 | + return content.HtmlTag("tr", _class); |
| 136 | + } |
| 137 | + public static string table(this string content, string _class = null) { |
| 138 | + return content.HtmlTag("table", _class); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | +} |
0 commit comments