Skip to content

Commit

Permalink
MVC - Helloworl
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 30, 2020
1 parent a4a4257 commit 7f6987a
Show file tree
Hide file tree
Showing 55 changed files with 40,254 additions and 0 deletions.
Binary file removed .DS_Store
Binary file not shown.
36 changes: 36 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// 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 (web)",
"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/mvc01_HelloWorld.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/.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}/mvc01_HelloWorld.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/mvc01_HelloWorld.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/mvc01_HelloWorld.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
72 changes: 72 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mvc01_HelloWorld.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace mvc01_HelloWorld.Controllers {
public class HomeController : Controller {
private readonly ILogger<HomeController> _logger;

public HomeController (ILogger<HomeController> logger) {
_logger = logger;

}

public string XinChao () => "Xin chào ASP.NET Core 3.x";

public IActionResult Index () {
ViewBag.dulieu1 = "Đây là dữ liệu 1"; //key=dulieu1, value = "Đây là dữ liệu 1"
ViewBag.dulieu2 = 12345;

// Một danh sách tênn các sản phầm
List<string> sanpham = new List<string>()
{
"Bàn ăn",
"Giường ngủ",
"Tủ áo"
};

return View (sanpham);
}

public IActionResult GetContent () {
// mime - tham khảo https://en.wikipedia.org/wiki/Media_type
string contentType = "text/plain";
string content = "Đây là nội dung file văn bản";
return Content (content, contentType, Encoding.UTF8);
}
public IActionResult TestRedirect () {

return Redirect ("https://xuanthulab.net"); // Redirect 302 - chuyển hướng sang URL khác
// return RedirectToRoute(new {controller="Home", action="Index"}); // Redirect 302 - Found
// return RedirectPermanent("https://xuanthulab.net"); // Redirect 301 - chuyển hướng URL khác
// return RedirectToAction("Index"); // Chuyển hướng sang Action khác
}
public IActionResult Sum (int x, int y) {
return Content ((x + y).ToString (), "text/plain", Encoding.UTF8);
}
public IActionResult GetJson () {
return Json (
new {
key1 = 100,
key2 = new string[] { "a", "b", "c" }
}
);
}

public IActionResult Privacy () {
return View ();
}

[ResponseCache (Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error () {
return View (new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
11 changes: 11 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace mvc01_HelloWorld.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
26 changes: 26 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace mvc01_HelloWorld
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
27 changes: 27 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:54545",
"sslPort": 44372
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"mvc01_HelloWorld": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
75 changes: 75 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Routing.Constraints;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Http;

namespace mvc01_HelloWorld {
public class Startup {

public Startup (IConfiguration configuration) {
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices (IServiceCollection services) {
services.AddControllersWithViews ();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment ()) {
app.UseDeveloperExceptionPage ();
} else {
app.UseExceptionHandler ("/Home/Error");
// Thông báo chỉ truy cập bằng https
app.UseHsts ();
}

// Middleware HttpsRedirection -> Chuyển hướng http sang https
app.UseHttpsRedirection ();

// StaticFileMiddleware - truy cập file tĩnh
app.UseStaticFiles ();

app.UseRouting ();

// AuthorizationMiddleware
app.UseAuthorization ();

// Tạo các Endpoint
app.UseEndpoints (endpoints => {
endpoints.MapControllerRoute (
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// endpoints.MapControllerRoute (
// name: "myroute", // đặt tên route
// defaults : new { controller = "Home", action = "Index" },
// constraints : new {
// id = @"\d+", // id phải có và phải là số
// title = new RegexRouteConstraint (new Regex (@"^[a-z0-9-]*$")) // title chỉ chứa số, chữ thường và ký hiệu -
// },
// pattern: "{title}-{id}.html");
endpoints.MapControllerRoute (
name: "myroute", // đặt tên route
defaults : new { controller = "Home", action = "Index" },
pattern: "{title:alpha:maxlength(8)}-{id:int}.html");
});
}
}
}
24 changes: 24 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@model List<string>

@{
Layout = null;
}
<!doctype html>
<html>
<head>
<title>Trang View đầu tiên</title>
</head>
<body>
<p>View .CSHTML là template <strong>Razor</strong> - nó chứa code C# bắt đầu bởi @@</p>
<p><i>@DateTime.Now.ToLongDateString() xuanthulab.net</i></p>
<p>@ViewBag.dulieu1</p>
<p>@ViewBag.dulieu2</p>

<ol>
@foreach (var sp in Model) {
<li>@sp</li>
}
</ol>

</body>
</html>
6 changes: 6 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Views/Home/Privacy.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>
25 changes: 25 additions & 0 deletions ASP_NET_CORE/mvc01_HelloWorld/Views/Shared/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@model ErrorViewModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
Loading

0 comments on commit 7f6987a

Please sign in to comment.