Skip to content

Commit

Permalink
LIB
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 30, 2020
1 parent a4a4257 commit 20cf28e
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 0 deletions.
42 changes: 42 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/htmlhelper/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/htmlhelper.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/htmlhelper.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/htmlhelper.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
143 changes: 143 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/htmlhelper/HtmlHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Http;

namespace XTLAB.HtmlHelper {

public static class HtmlHelper {
/// <summary>
/// Phát sinh trang HTML
/// </summary>
/// <param name="title">Tiêu đề trang</param>
/// <param name="content">Nội dung trong thẻ body</param>
/// <returns>Trang HTML</returns>
public static string HtmlDocument (string title, string content) {
return $@"
<!DOCTYPE html>
<html>
<head>
<meta charset=""UTF-8"">
<title>{title}</title>
<link rel=""stylesheet"" href=""/css/site.min.css"" />
<script src=""/js/jquery.min.js"">
</script><script src=""/js/popper.min.js"">
</script><script src=""/js/bootstrap.min.js""></script>
</head>
<body>
{content}
</body>
</html>";
}

/// <summary>
/// Phát sinh HTML thanh menu trên, menu nào active phụ thuộc vào URL mà request gủi đến
/// </summary>
/// <param name="menus">Mảng các menu item, mỗi item có cấu trúc {url, lable}</param>
/// <param name="request">HttpRequest</param>
/// <returns></returns>

public static string MenuTop (object[] menus, HttpRequest request) {

var menubuilder = new StringBuilder ();
menubuilder.Append ("<ul class=\"navbar-nav\">");
foreach (dynamic menu in menus) {
string _class = "nav-item";
// Active khi request.PathBase giống url của menu
if (request.PathBase == menu.url) _class += " active";
menubuilder.Append ($@"
<li class=""{_class}"">
<a class=""nav-link"" href=""{menu.url}"">{menu.label}</a>
</li>
");
}
menubuilder.Append ("</ul>\n");

string menuhtml = $@"
<div class=""container"">
<nav class=""navbar navbar-expand-lg navbar-dark mainbackground"">
<a class=""navbar-brand"" href=""/"">XTLAB</a>
<button class=""navbar-toggler"" type=""button""
data-toggle=""collapse"" data-target=""#my-nav-bar""
aria-controls=""my-nav-bar"" aria-expanded=""false"" aria-label=""Toggle navigation"">
<span class=""navbar-toggler-icon""></span>
</button>
<div class=""collapse navbar-collapse"" id=""my-nav-bar"">
{menubuilder}
</div>
</nav></div>";

return menuhtml;
}

/// <summary>
/// Những menu item mặc định cho trang
/// </summary>
/// <returns>Mảng các menuitem</returns>
public static object[] DefaultMenuTopItems () {
return new object[] {
new {
url = "/RequestInfo",
label = "Request"
},
new {
url = "/Form",
label = "Form"
},
new {
url = "/Encoding",
label = "Encoding"
},
new {
url = "/Cookies",
label = "Cookies"
},
new {
url = "/Json",
label = "JSON"
}
};
}

public static string HtmlTrangchu () {
return $@"
<div class=""container"">
<div class=""jumbotron"">
<h1 class=""display-4"">Đây là một trang Web .NET Core</h1>
<p class=""lead"">Trang Web này xây dựng trên nền tảng <code>.NET Core</code>,
chưa sử dụng kỹ thuật MVC - nhằm mục đích học tập.
Mã nguồn trang này tại <a target=""_blank""
href=""https://github.com/xuanthulabnet/learn-cs-netcore/blob/master/ASP_NET_CORE/03.RequestResponse/"">
Mã nguồn Ví dụ</a>
</p>
<hr class=""my-4"">
<p><code>.NET Core</code> là một hệ thống chạy đa nền tảng (Windows, Linux, macOS)</p>
<a class=""btn btn-danger btn-lg"" href=""https://xuanthulab.net/lap-trinh-c-co-ban/"" role=""button"">Xem thêm</a>
</div>
</div>
";

}

// Mở rộng String, phát sinh thẻ HTML với nội dụng là String
// Ví dụ:
// "content".HtmlTag() => <p>content</p>
// "content".HtmlTag("div", "text-danger") => <div class="text-danger">content</div>
public static string HtmlTag (this string content, string tag = "p", string _class = null) {
string cls = (_class != null) ? $" class=\"{_class}\"" : null;
return $"<{tag + cls}>{content}</{tag}>";
}
public static string td (this string content, string _class = null) {
return content.HtmlTag ("td", _class);
}
public static string tr (this string content, string _class = null) {
return content.HtmlTag ("tr", _class);
}
public static string table (this string content, string _class = null) {
return content.HtmlTag ("table", _class);
}

}
}
16 changes: 16 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/htmlhelper/htmlhelper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageId>XTL.HtmlHelper</PackageId>
<Version>1.0.0</Version>
<Authors>xuanthulab</Authors>
<Company>XUANTHULAB.NET</Company>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/htmltestcode/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/htmltestcode.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/htmltestcode/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/htmltestcode.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/htmltestcode.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/htmltestcode.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
14 changes: 14 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/htmltestcode/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using XTLAB.HtmlHelper;

namespace htmltestcode
{
class Program
{
static void Main(string[] args)
{
String html = "Ví dụ sử dụng HtmlHelper".HtmlTag("div", "text-danger");
Console.WriteLine(html);
}
}
}
12 changes: 12 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/htmltestcode/htmltestcode.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\htmlhelper\htmlhelper.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
3 changes: 3 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Tạo thư viện C# NET Core và chia sẻ lên nuget.org

https://xuanthulab.net/tao-thu-vien-c-net-core-va-chia-se-len-nuget-org.html
27 changes: 27 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/test/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/test.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/test/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/test.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/test.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/test.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
13 changes: 13 additions & 0 deletions ASP_NET_CORE/htmlhelperlib/test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using XTLAB;
namespace test
{
class Program
{
static void Main(string[] args)
{
"abc".HtmlTag("p");
Console.WriteLine("Hello World!");
}
}
}
Loading

0 comments on commit 20cf28e

Please sign in to comment.