Skip to content

Commit 0aee6e1

Browse files
authored
Session 0203 (#61)
* Started * Form interaction demos * Updated notes
1 parent 18fc1f4 commit 0aee6e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+40468
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/src/MyCoolApp.Web/bin/Debug/net5.0/MyCoolApp.Web.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}/src/MyCoolApp.Web",
16+
"stopAtEntry": false,
17+
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
18+
"serverReadyAction": {
19+
"action": "openExternally",
20+
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
21+
},
22+
"env": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
},
25+
"sourceFileMap": {
26+
"/Views": "${workspaceFolder}/Views"
27+
}
28+
},
29+
{
30+
"name": ".NET Core Attach",
31+
"type": "coreclr",
32+
"request": "attach",
33+
"processId": "${command:pickProcess}"
34+
}
35+
]
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/src/MyCoolApp.Web/MyCoolApp.Web.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/src/MyCoolApp.Web/MyCoolApp.Web.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"${workspaceFolder}/src/MyCoolApp.Web/MyCoolApp.Web.csproj",
36+
"/property:GenerateFullPaths=true",
37+
"/consoleloggerparameters:NoSummary"
38+
],
39+
"problemMatcher": "$msCompile"
40+
}
41+
]
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# ASP.NET Core MVC - Part 2: Forms, Configuration, and Logging
2+
3+
In our [last session](../0202-ModelViewController/README.md) we covered the basics of ASP.NET Core MVC. We learned how to use controllers, views, and models to present a simple website. In this continuation of the topic of MVC, we will learn about working with forms, the Configuration Model, and logging.
4+
5+
## Interacting with Forms
6+
7+
HTML makes a series of user interface options available for the browser to send data to the server for processing. The `<input>`, `<textarea>` and `<select>` tags provide the basics for our viewers to interact with, and can be created from our Views easily, but how do we configure a Controller with actions to receive this data?
8+
9+
Let's create a form to allow interaction with the server. We'll use the [Post-Redirect-Get pattern](https://wikipedia.org/wiki/Post/Redirect/Get) to model our views and controller.
10+
11+
### Step 1: The Form
12+
13+
We can place a form on any View that is returned from our application, and use the standard HTML `<form>` tag to represent that interaction. However, there are a few ways that we can create the form using ASP.NET resources that will make it more resilient to changes in our application.
14+
15+
For the first sample, let's use an HTML helper to post a simple search string to the application.
16+
17+
```razor
18+
@using (Html.BeginForm()) {
19+
20+
<div class="input-group">
21+
22+
<input type="text" name="Search" class="form-control" placeholder="Search..." />
23+
<input type="submit" class="btn-primary" value="Go!" />
24+
25+
</div>
26+
27+
}
28+
```
29+
30+
### Step 2: The Actions
31+
32+
### Validation
33+
34+
ModelState / Validation - https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0
35+
36+
## Configuration
37+
38+
IConfiguration
39+
40+
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0
41+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using MyCoolApp.Web.Models;
3+
4+
namespace MyCoolApp.Web.Controllers
5+
{
6+
7+
public class FormController : Controller
8+
{
9+
10+
public IActionResult Basic1()
11+
{
12+
return View();
13+
}
14+
15+
[HttpPost]
16+
public IActionResult BasicForm(string search)
17+
{
18+
19+
TempData.Add("SearchTerm", search);
20+
return RedirectToAction("Basic1");
21+
22+
}
23+
24+
public IActionResult Model() {
25+
return View();
26+
}
27+
28+
[HttpPost]
29+
public IActionResult Model(Person p) {
30+
31+
TempData.Put("Person", p);
32+
return RedirectToAction("Model");
33+
34+
}
35+
36+
public IActionResult Validate() {
37+
return View();
38+
}
39+
40+
[HttpPost]
41+
public IActionResult Validate(Person p) {
42+
43+
if (ModelState.IsValid) {
44+
TempData.Put("Person", p);
45+
return RedirectToAction("Validate");
46+
}
47+
48+
return View(p);
49+
50+
}
51+
52+
}
53+
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Extensions.Logging;
8+
using MyCoolApp.Web.Models;
9+
10+
namespace MyCoolApp.Web.Controllers
11+
{
12+
public class HomeController : Controller
13+
{
14+
private readonly ILogger<HomeController> _logger;
15+
16+
public HomeController(ILogger<HomeController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
public IActionResult Index()
22+
{
23+
return View();
24+
}
25+
26+
public IActionResult Privacy()
27+
{
28+
return View();
29+
}
30+
31+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
32+
public IActionResult Error()
33+
{
34+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
35+
}
36+
}
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace MyCoolApp.Web.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace MyCoolApp.Web.Models
5+
{
6+
7+
[Serializable]
8+
public class Person
9+
{
10+
11+
[Display(Name = "Name")]
12+
[Required]
13+
public string Name { get; set; }
14+
15+
[Display(Name = "City")]
16+
public string City { get; set; }
17+
18+
[Display(Name = "Age")]
19+
[Range(3, 80)]
20+
public int Age { get; set; }
21+
22+
}
23+
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace MyCoolApp.Web
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:27802",
7+
"sslPort": 44395
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"MyCoolApp.Web": {
19+
"commandName": "Project",
20+
"dotnetRunMessages": "true",
21+
"launchBrowser": true,
22+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)