Skip to content

Commit af79288

Browse files
added manual Action results, create custom IActionResult
1 parent 26b6daf commit af79288

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text;
45
using System.Threading.Tasks;
6+
using ControllersAndActions.Inframstructure;
57
using Microsoft.AspNetCore.Mvc;
68

79

@@ -10,10 +12,42 @@ namespace ControllersAndActions.Controllers
1012
{
1113
public class HomeController : Controller
1214
{
13-
14-
public IActionResult Index()
15+
//renders a form asking for user input of name and city
16+
public ViewResult Index()
1517
{
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}"};
1751
}
1852
}
1953
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
namespace ControllersAndActions.Inframstructure
9+
{
10+
public class CustomHtmlResult : IActionResult
11+
{
12+
public string Content { get; set; }
13+
14+
public Task ExecuteResultAsync(ActionContext context)
15+
{
16+
context.HttpContext.Response.StatusCode = 200;
17+
context.HttpContext.Response.ContentType = "text/html";
18+
byte[] content = Encoding.ASCII.GetBytes(Content);
19+
return context.HttpContext.Response.Body.WriteAsync(content, 0, content.Length);
20+
}
21+
}
22+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="m-1 p-1">
2-
<form asp-action="ReceiveForm" method="post">
2+
<form asp-action="ManualReceiveForm" method="post">
33
<div class="form-group">
44
<lable for="name">Name: </lable>
55
<input name="name" class="form-control" />
@@ -8,7 +8,7 @@
88
<lable for="city">City: </lable>
99
<input name="city" class="form-control" />
1010
</div>
11-
<button class="btn btn-primary center-block" type="submit"></button>
11+
<button class="btn btn-primary center-block" type="submit">Submit</button>
1212
</form>
1313

1414
</div>

0 commit comments

Comments
 (0)