From f657b1f97da8ffbee2cd2dea2f3244b6c09c1117 Mon Sep 17 00:00:00 2001 From: gorohoroh Date: Tue, 3 Jul 2018 21:00:50 +0300 Subject: [PATCH] Action results: deriving from the Controller base class, modifying an action to return IActionResult, creating and instantiating a Restaurant model, and returning the model instance wrapped in a new ObjectResult --- .../Controllers/HomeController.cs | 11 +++++++--- .../OdeToFoodRider/Models/Restaurant.cs | 8 ++++++++ .../Controllers/HomeController.cs | 20 ++++++++++++------- .../Models/Restaurant.cs | 13 ++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs create mode 100644 OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs diff --git a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs index c93c2c4..4153d72 100644 --- a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs +++ b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs @@ -1,10 +1,15 @@ +using Microsoft.AspNetCore.Mvc; +using OdeToFoodRider.Models; + namespace OdeToFoodRider.Controllers { - public class HomeController + public class HomeController : Controller { - public string Index() + public IActionResult Index() { - return "Hello from Rider's Home controller!"; + var model = new Restaurant() {Id = 1, Name = "Scott's Pizza Place"}; + + return new ObjectResult(model); } } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs b/OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs new file mode 100644 index 0000000..67b85df --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs @@ -0,0 +1,8 @@ +namespace OdeToFoodRider.Models +{ + public class Restaurant + { + public int Id { get; set; } + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs index 41bd26c..a5adf4e 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs @@ -1,15 +1,21 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using OdeToFoodVisualStudio.Models; namespace OdeToFoodVisualStudio.Controllers { - public class HomeController + public class HomeController : Controller { - public string Index() + public IActionResult Index() { - return "Hello from Visual Studio's Home controller"; + var model = new Restaurant { Id = 1, Name = "Scott's Pizza Place" }; + + return new ObjectResult(model); + + // VSRD: Visual Studio doesn't have a quick action to change return type if you try to return something that doesn't match it + // VSRD: Visual Studio's take on Extend/Shrink Selection (Shift+Alt+Plus/Minus) can't extend to a string literal except quotes, + // making it non-trivial to paste text into string literals - it can only extend to the whole string literal + //return Content("Hello from Visual Studio's Home controller"); + } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs new file mode 100644 index 0000000..3423f6b --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace OdeToFoodVisualStudio.Models +{ + public class Restaurant + { + public int Id { get; set; } + public string Name { get; set; } + } +}