1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Text ;
4
5
using System . Threading . Tasks ;
6
+ using ControllersAndActions . Inframstructure ;
5
7
using Microsoft . AspNetCore . Mvc ;
6
8
7
9
@@ -10,10 +12,42 @@ namespace ControllersAndActions.Controllers
10
12
{
11
13
public class HomeController : Controller
12
14
{
13
-
14
- public IActionResult Index ( )
15
+ //renders a form asking for user input of name and city
16
+ public ViewResult Index ( )
15
17
{
16
- return View ( ) ;
18
+ return View ( "SimpleForm" ) ;
19
+ }
20
+
21
+
22
+
23
+
24
+ //items from form are retrieved from the Request object, from the Form property
25
+ public ViewResult ReceiveFormData ( )
26
+ {
27
+ var name = Request . Form [ "name" ] ;
28
+ var city = Request . Form [ "city" ] ;
29
+ return View ( "Result" , $ "{ name } lives in { city } ") ;
30
+ }
31
+ //items from form are retrieved through the method params of the same name as input tag name values.
32
+ public ViewResult ReceiveFormDataAsArgs ( string name , string city )
33
+ {
34
+ return View ( "Result" , $ "{ name } lives in { city } ") ;
35
+ }
36
+
37
+ //manually sending html to the the browser, after recieveing the form data as args to the method; UTH preview of what action methods do
38
+ public void ManualReceiveForm ( string name , string city )
39
+ {
40
+ Response . StatusCode = 200 ;
41
+ Response . ContentType = "text/html" ;
42
+ byte [ ] content = Encoding . ASCII . GetBytes ( $ "<html><body>{ name } lives in { city } </body>") ;
43
+ Response . Body . WriteAsync ( content , 0 , content . Length ) ;
44
+ }
45
+ //instead of manually writing the code above -- let's encapsulate it implementing the IActionResult interface, essentially making our own ActionResult
46
+ //Goto the CustomHtmlResult file in the Infrastructure folder to see the same code encapsulated into a method using the interface
47
+ //now we can use that method with just the code below, returning an IActionResult -- just like all the other IActionResult methods in the frameworkS
48
+ public IActionResult ReceiveFormMyCustomAction ( string name , string city )
49
+ {
50
+ return new CustomHtmlResult { Content = $ "{ name } lives in { city } "} ;
17
51
}
18
52
}
19
53
}
0 commit comments