SlideShare a Scribd company logo
BIT 286:
Web Applications
ASP.Net Core Web API - Basics
2
Agenda
• Talk about requirements doc
• We will do ‘planning poker’ on the day that this is due
• Right now: identify users
• Start on collecting user stories
• ASP.Net Core Web API
• Brainstorming Quiz questions
About 20 minutes before class ends:
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
3
Communication – you must use Slack
• It’s industry standard
• We covered it in class
• It doesn’t have to be the only thing you use
• But I need to see you demonstrate mastery and confidence with this
important tool.
• For right now: “Please use it!”
• If I have to “Use it or lose points!”
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
4
Brainstorming Quiz questions
• About 20 minutes before class ends:
• Join the BIT286 class Slack team (link in Lesson 04, on the 286 website)
• Y’all will brainstorm quiz questions for about 10 minutes
• Anything you might want to see on a quiz you should post to #quiz-questions
• I’ll be posting stuff, too
• We’ll go over ‘likely’ candidate questions in the last 10 minutes of class
• Quiz at the start of next lecture
• Questions will be drawn primarily BUT NOT EXCLUSIVELY from our brainstorm
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
5
User Requirements
• <To be discussed during class>
• If you’re learning a new technology, add work items into your
tracking, too
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
6
Objectives
• Try creating a simple REST based web application, using ASP.Net
• Use ASP.Net Core Web API
• Use Postman to test the REST API
• Instead of creating a full-on JS client
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
7
Review: REST API architecture
• From
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
8
.Net: Core vs. Framework
• Tl;dr: .Net Framework is last-gen. It’s being replaced with .Net Core
• VB  Web Forms  MVC
• We’re going to be using .Net Core in this class
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
These slides are mostly a walkthrough of https://docs.microsoft.com/en-us/as
pnet/core/tutorials/first-web-api
9
Installing the software
10
Install Visual Studio 2017
• You’ll need to add the ‘NuGet package
manager’, plus the “ASP.Net and web
development” workload
• Run the VS 2017 Installer, then click on the ‘Modify’
button
• Note: this may take a long time
• We’ll also use Postman
• There’s a Chrome app for this
• https://chrome.google.com/webstore/detail/postman
/fhbjgbiflinjbdggehcddcbncdddomop
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
11
Basic Web API project
• We’re going to follow along with
https://docs.microsoft.com/
en-us/aspnet/core/
tutorials/first-web-api
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
12
Create the Solution
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
13
Use the Web API template
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
14
Install a test Database
• InMemory DB
• all records stored in memory
• You’ll lose all data when you quit the program
• Great for testing, terrible for real use ☺
• https://docs.microsoft.com/en-us/ef/core/providers/in-memory/
• We’ll do this using NuGet
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
15
Install a test
Database
• We’ll install it using NuGet
• NuGet is a package manager
for Visual Studio
• It’s included >= VS 2012
• NuGet:
• Right-click on the project
• Select ‘
• More details at
https://docs.microsoft.com
/en-us/nuget/tools/packag
e-manager-ui
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
16
Add the test DB
• Click on ‘Browse’, then paste in
Microsoft.EntityFrameworkCore.InMemory
• When you mouse over it you’ll see the ‘install’ icon appear
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
17
Add the test DB
• At this point the
software is installed
• We just need to tell this
particular project to use
it
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
18
Modify the .csproj file
• Make sure this line is present:
• (for me it was already there when I went looking for it)
• <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools"
Version="1.0.0" />
</ItemGroup>
</Project>
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
These slides are mostly a walkthrough of https://docs.microsoft.com/en-us/as
pnet/core/tutorials/first-web-api
19
Add Database support
20
Create a Models folder
• There’s no ‘Models’ folder, so let’s make one
• Right click on the project, then AddNew Folder:
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
21
Add a Todo.cs to the Models folder
namespace WebAPI_Demo.Models // This will be
specific to you
{ public class TodoItem
{ [Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)
]
public long Key { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; {}
}
} These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
22
Examine code, Make sure it compiles
• A Todo object represents a single row in the database
• Looking at the code:
• From https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api:
• “The [Key] data annotation denotes the property, Key, is a unique identifier.”
• “[DatabaseGenerated specifies the database will generate the key (rather than the
application).”
• “DatabaseGeneratedOption.Identity specifies the database should generate integer keys
when a row is inserted.”
• Build the program: Build  Build Solution
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
23
Add a DbContext
• From
http://www.entityframeworktutorial.net/EntityFramework4.3/dbcontext-
vs-objectcontext.aspx
:
• Handles all the translation between your objects and the actual database
• For example, given a LINQ-to-Entity it will generate the SQL to
query/add/update/delete rows in the database
• Add the file
• Right click on the ‘Models’ folder
• Add Class
• Name it TodoContext.cs (so it’s easy to find )
☺
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
24
Add a DB context
using Microsoft.EntityFrameworkCore;
namespace WebAPI_Demo.Models
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options)
: base(options)
{
}
public DbSet<TodoItem> TodoItems { get; set; }
}
}
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
25
How does TodoContext work?
• It inherits from the DbContext, which does all the work
public class TodoContext : DbContext
{
• DbContext must use magic reflection to inspect each object in order to
be able to know how to map it to SQL and back
• General description: Wikipedia
• Warning: a bit dense – skim it to get the gist of it, but don’t worry about details
• C#-specific docs
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
26
Next step: add a repository class
• First, this is a terrible re-use of the word repository
• Second, this is implemented via dependency injection
• What is Dependency Injection?
• Let’s say that you’ve got two classes, Car and Engine.
• Normally each Car object will create an Engine for itself, usually in the constructor
• We can say that the Car object depends on (it uses) the Engine object
• Instead of having the Car create an Engine object, we will give it an Engine to use.
AKA, we will inject the Engine object into the Car, which the Car then uses (the Car
then depends on).
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
27
Next step: add a repository class
• Key Benefit: We can now replace the Engine with other classes that also
behave like Engines
• Example: We could test our Car using a TestEngine which also logs the things it’s being
told to do. We then check the log later to make sure the the Car was telling the
Engine the correct things
• Example: What if the Car object can save itself to a database? With DI, we could give
it a different database object, and save itself into a different DB
(for example, an Oracle DB instead of a MySQL)
• Google for ‘dependency injection simple explanation’
• ‘Simple’ explanation #1
• ‘Simple’ explanation #2
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
28
Create the Repository class
( ITodoRepository.cs, in the Models folder)
using System.Collections.Generic;
namespace WebAPI_Demo.Models
{
public interface ITodoRepository
{
void Add(TodoItem item);
IEnumerable<TodoItem> GetAll();
TodoItem Find(long key);
void Remove(long key);
void Update(TodoItem item);
}
}
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
29
Should we review interfaces here? ☺
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
30
TodoRepository.cs (also in Models folder)
• Entire code is right above
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api#re
gister-the-repository
• From the same link:
• “By defining a repository interface, we can decouple the repository class from
the MVC controller that uses it. Instead of instantiating a TodoRepository inside
the controller we will inject an ITodoRepository using the built-in support in
ASP.NET Core for dependency injection.”
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
31
Repository:
Inheritance
using System.Collections.Generic;
using System.Linq;
namespace WebAPI_Demo.Models
{
public class TodoRepository : ITodoRepository
{
private readonly TodoContext _context;
public TodoRepository(TodoContext context)
{
_context = context;
if (_context.TodoItems.Count() == 0)
Add(new TodoItem { Name = "Item1" });
}
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
32
Implementing the ITodoRepository interface
1 / 2
public IEnumerable<TodoItem> GetAll()
{
return _context.TodoItems.ToList();
}
public void Add(TodoItem item)
{
_context.TodoItems.Add(item);
_context.SaveChanges();
}
public TodoItem Find(long key)
{
return _context.TodoItems.FirstOrDefault(t => t.Key == key);
}
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
33
Implementing the ITodoRepository interface
2 / 2
public void Remove(long key)
{
var entity = _context.TodoItems.First(t => t.Key == key);
_context.TodoItems.Remove(entity);
_context.SaveChanges();
}
public void Update(TodoItem item)
{
_context.TodoItems.Update(item);
_context.SaveChanges();
}
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
34
Register the repository
• In Startup.cs, find the method named ConfigureServices()
• Replace it with
public void ConfigureServices(IServiceCollection
services)
{
services.AddDbContext<TodoContext>(opt =>
opt.UseInMemoryDatabase());
services.AddMvc(); // already present
services.AddScoped<ITodoRepository,
TodoRepository>();
}
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
35
Register the repository
• In Startup.cs, add the following to the top of the file:
using WebAPI_Demo.Models;
using Microsoft.EntityFrameworkCore;
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
36
Dependency Injection
• https://davidzych.com/dependency-injection-in-asp-net-vnext/
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
These slides are mostly a walkthrough of https://docs.microsoft.com/en-us/as
pnet/core/tutorials/first-web-api
37
Adding a controller
38
Add the controller file
• Right click on the Controllers folder, Add  New Item
• You want MVC Controller class.
• Make sure it’s for .Net Core
• Name it TodoController.cs
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
39
I’m going to switch it up from here
• Creating the slides takes a lot of time
• And it’s all being copied from that tutorial page anyways
• So instead of showing you everything, we’re going to try something
new:
•  Slides will be used as notes
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
40
Copy from tutorial
• From
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api
#getting-to-do-items
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
41
Controller - Routing
• Routing can be done with attributes:
[Route("api/[controller]")]
public class TodoController : Controller
• [controller] placeholder:
• The class name minus the ‘Controller’ part
• “TodoController” – “Controller” = “Todo”
• URLs will start with <server info>/api/todo
• NOTE: Routes are case INsensitive in ASP.Net Core – Todo, todo, ToDo, tOdO
all work
• Example:
• http://localhost:51070/api/todo
<server info>/api/[controller]
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
42
Controller - Routing
• More Details:
• Attribute Routing Overall
• Placeholders: The [controller] thing
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
43
Routing details
[HttpGet] // matches /api/[controller]
public IEnumerable<TodoItem> GetAll()
{ return _todoRepository.GetAll(); }
• Example: http://localhost:51070/api/todo - goes to GetAll
• Example: http://localhost:51070/api/todo/3 - goes to GetById
[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(long id)
{ // ‘Name’ in HttpGet is for internal (code)
purposes
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
44
Run it
• Control + F5, then wait for the browser to appear
• Let’s play experiment with the browser results a bit
• WARNING: when your browser window closes, the web server shuts down
• Then let’s experiment with Postman for a bit
• With the browser window still open ☺
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
45
Create
• ‘CreatedAtRoute’ is a method that returns an object that will
eventually become an HTTP 201 response
• This includes a ‘Location’ field
• Postman – POST
• {
"name":"walk dog",
"isComplete":true
}
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
46
Update
• Note – must send the entire object
• Can use HTTP PATCH for just sending the delta
• We’re not handling PATCH requests here
• Postman
• PUT request
• http://localhost:51071/api/todo/2  2 is the id of the thing to update
• Must include the entire object (including ‘key’, which is weird )
☺
• {
"key": "2",
"name":"walk dog woooo it’s chaaaaaaanged!“,
"isComplete":false
}
• 204 (No Content) is the ‘success’ response
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
47
Delete
• Postman – DELETE verb
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
48
Debugging
• Set a break point in Visual Studio
• Launch the ASP.Net Core web app via Debug  Start Debugging
• Have the browser (or Postman) make a request that will execute the
breakpointed code
• Visual Studio auto-magically stops where your breakpoint is!
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
49
Where to go next?
• Logging
• Get information from your web app about what’s happening on the server
• Critical for debugging
• Can be set up to be viewed in Visual Studio
• Client – JavaScipt
• jQuery?
• Angular?
• React?
• Vue?
• Note: Frameworks are arranged in descending order of instructor familiarity ☺
These slides are mostly a walkthrough of https://docs.microsoft.com/en-
us/aspnet/core/tutorials/first-web-api
Ad

Recommended

Basics Of Introduction to ASP.NET Core.pptx
Basics Of Introduction to ASP.NET Core.pptx
1rajeev1mishra
 
Getting started with dotnet core Web APIs
Getting started with dotnet core Web APIs
Knoldus Inc.
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Akhil Mittal
 
ASP.NET Core in Action (2018).pdf
ASP.NET Core in Action (2018).pdf
Emma Burke
 
767458168-Introduction-to-Web-API767458168-Introduction-to-Web-API.pdf.pdf
767458168-Introduction-to-Web-API767458168-Introduction-to-Web-API.pdf.pdf
ssuser16fbed
 
ASP.NET Core 1.0
ASP.NET Core 1.0
Ido Flatow
 
ASP NET Web API 2 Building a REST Service from Start to Finish 2nd Edition Ja...
ASP NET Web API 2 Building a REST Service from Start to Finish 2nd Edition Ja...
prienmance8p
 
Asp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework Core
mohamed elshafey
 
ASP.NET Core Demos Part 2
ASP.NET Core Demos Part 2
Erik Noren
 
Asp.net core tutorial
Asp.net core tutorial
HarikaReddy115
 
Introduction-to-ASPNET-Core ASP.NET.pptx
Introduction-to-ASPNET-Core ASP.NET.pptx
MAHERMOHAMED27
 
Building HTTP APIs with ASP.NET Core
Building HTTP APIs with ASP.NET Core
Filip W
 
ASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web application
AMARAAHMED7
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi Applications
Effie Arditi
 
Will be an introduction to
Will be an introduction to
Sayed Ahmed
 
Murach: An introduction to web programming with ASP.NET Core MVC
Murach: An introduction to web programming with ASP.NET Core MVC
MahmoudOHassouna
 
Building Web Apis With Aspnet Core Meap Version 11 All 12 Chapters Valerio De...
Building Web Apis With Aspnet Core Meap Version 11 All 12 Chapters Valerio De...
anjumlehde00
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0
Steven Smith
 
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher
 
White Paper : ASP.NET Core AngularJs 2 and Prime
White Paper : ASP.NET Core AngularJs 2 and Prime
Hamida Rebai Trabelsi
 
REST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web API
Tomas Jansson
 
ASP.NET
ASP.NET
Chandan Gupta Bhagat
 
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
Scholarhat
 
Progressive Web Apps and React
Progressive Web Apps and React
Mike Melusky
 
Quick Interview Preparation Dot Net Core
Quick Interview Preparation Dot Net Core
Karmanjay Verma
 
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
MskDotNet Community
 
.NET Core Today and Tomorrow
.NET Core Today and Tomorrow
Jon Galloway
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
Adnan Masood
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 

More Related Content

Similar to ASP.net web api Power Point Presentation (20)

ASP.NET Core Demos Part 2
ASP.NET Core Demos Part 2
Erik Noren
 
Asp.net core tutorial
Asp.net core tutorial
HarikaReddy115
 
Introduction-to-ASPNET-Core ASP.NET.pptx
Introduction-to-ASPNET-Core ASP.NET.pptx
MAHERMOHAMED27
 
Building HTTP APIs with ASP.NET Core
Building HTTP APIs with ASP.NET Core
Filip W
 
ASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web application
AMARAAHMED7
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi Applications
Effie Arditi
 
Will be an introduction to
Will be an introduction to
Sayed Ahmed
 
Murach: An introduction to web programming with ASP.NET Core MVC
Murach: An introduction to web programming with ASP.NET Core MVC
MahmoudOHassouna
 
Building Web Apis With Aspnet Core Meap Version 11 All 12 Chapters Valerio De...
Building Web Apis With Aspnet Core Meap Version 11 All 12 Chapters Valerio De...
anjumlehde00
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0
Steven Smith
 
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher
 
White Paper : ASP.NET Core AngularJs 2 and Prime
White Paper : ASP.NET Core AngularJs 2 and Prime
Hamida Rebai Trabelsi
 
REST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web API
Tomas Jansson
 
ASP.NET
ASP.NET
Chandan Gupta Bhagat
 
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
Scholarhat
 
Progressive Web Apps and React
Progressive Web Apps and React
Mike Melusky
 
Quick Interview Preparation Dot Net Core
Quick Interview Preparation Dot Net Core
Karmanjay Verma
 
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
MskDotNet Community
 
.NET Core Today and Tomorrow
.NET Core Today and Tomorrow
Jon Galloway
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
Adnan Masood
 
ASP.NET Core Demos Part 2
ASP.NET Core Demos Part 2
Erik Noren
 
Introduction-to-ASPNET-Core ASP.NET.pptx
Introduction-to-ASPNET-Core ASP.NET.pptx
MAHERMOHAMED27
 
Building HTTP APIs with ASP.NET Core
Building HTTP APIs with ASP.NET Core
Filip W
 
ASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web application
AMARAAHMED7
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi Applications
Effie Arditi
 
Will be an introduction to
Will be an introduction to
Sayed Ahmed
 
Murach: An introduction to web programming with ASP.NET Core MVC
Murach: An introduction to web programming with ASP.NET Core MVC
MahmoudOHassouna
 
Building Web Apis With Aspnet Core Meap Version 11 All 12 Chapters Valerio De...
Building Web Apis With Aspnet Core Meap Version 11 All 12 Chapters Valerio De...
anjumlehde00
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0
Steven Smith
 
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher
 
White Paper : ASP.NET Core AngularJs 2 and Prime
White Paper : ASP.NET Core AngularJs 2 and Prime
Hamida Rebai Trabelsi
 
REST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web API
Tomas Jansson
 
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
Scholarhat
 
Progressive Web Apps and React
Progressive Web Apps and React
Mike Melusky
 
Quick Interview Preparation Dot Net Core
Quick Interview Preparation Dot Net Core
Karmanjay Verma
 
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
MskDotNet Community
 
.NET Core Today and Tomorrow
.NET Core Today and Tomorrow
Jon Galloway
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
Adnan Masood
 

Recently uploaded (20)

ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
IObit Driver Booster Pro 12 Crack Latest Version Download
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
IObit Driver Booster Pro 12 Crack Latest Version Download
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Ad

ASP.net web api Power Point Presentation

  • 1. BIT 286: Web Applications ASP.Net Core Web API - Basics
  • 2. 2 Agenda • Talk about requirements doc • We will do ‘planning poker’ on the day that this is due • Right now: identify users • Start on collecting user stories • ASP.Net Core Web API • Brainstorming Quiz questions About 20 minutes before class ends: These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 3. 3 Communication – you must use Slack • It’s industry standard • We covered it in class • It doesn’t have to be the only thing you use • But I need to see you demonstrate mastery and confidence with this important tool. • For right now: “Please use it!” • If I have to “Use it or lose points!” These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 4. 4 Brainstorming Quiz questions • About 20 minutes before class ends: • Join the BIT286 class Slack team (link in Lesson 04, on the 286 website) • Y’all will brainstorm quiz questions for about 10 minutes • Anything you might want to see on a quiz you should post to #quiz-questions • I’ll be posting stuff, too • We’ll go over ‘likely’ candidate questions in the last 10 minutes of class • Quiz at the start of next lecture • Questions will be drawn primarily BUT NOT EXCLUSIVELY from our brainstorm These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 5. 5 User Requirements • <To be discussed during class> • If you’re learning a new technology, add work items into your tracking, too These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 6. 6 Objectives • Try creating a simple REST based web application, using ASP.Net • Use ASP.Net Core Web API • Use Postman to test the REST API • Instead of creating a full-on JS client These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 7. 7 Review: REST API architecture • From https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 8. 8 .Net: Core vs. Framework • Tl;dr: .Net Framework is last-gen. It’s being replaced with .Net Core • VB  Web Forms  MVC • We’re going to be using .Net Core in this class These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 9. These slides are mostly a walkthrough of https://docs.microsoft.com/en-us/as pnet/core/tutorials/first-web-api 9 Installing the software
  • 10. 10 Install Visual Studio 2017 • You’ll need to add the ‘NuGet package manager’, plus the “ASP.Net and web development” workload • Run the VS 2017 Installer, then click on the ‘Modify’ button • Note: this may take a long time • We’ll also use Postman • There’s a Chrome app for this • https://chrome.google.com/webstore/detail/postman /fhbjgbiflinjbdggehcddcbncdddomop These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 11. 11 Basic Web API project • We’re going to follow along with https://docs.microsoft.com/ en-us/aspnet/core/ tutorials/first-web-api These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 12. 12 Create the Solution These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 13. 13 Use the Web API template These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 14. 14 Install a test Database • InMemory DB • all records stored in memory • You’ll lose all data when you quit the program • Great for testing, terrible for real use ☺ • https://docs.microsoft.com/en-us/ef/core/providers/in-memory/ • We’ll do this using NuGet These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 15. 15 Install a test Database • We’ll install it using NuGet • NuGet is a package manager for Visual Studio • It’s included >= VS 2012 • NuGet: • Right-click on the project • Select ‘ • More details at https://docs.microsoft.com /en-us/nuget/tools/packag e-manager-ui These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 16. 16 Add the test DB • Click on ‘Browse’, then paste in Microsoft.EntityFrameworkCore.InMemory • When you mouse over it you’ll see the ‘install’ icon appear These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 17. 17 Add the test DB • At this point the software is installed • We just need to tell this particular project to use it These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 18. 18 Modify the .csproj file • Make sure this line is present: • (for me it was already there when I went looking for it) • <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> </PropertyGroup> <ItemGroup> <Folder Include="wwwroot" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="1.1.1" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" /> </ItemGroup> </Project> These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 19. These slides are mostly a walkthrough of https://docs.microsoft.com/en-us/as pnet/core/tutorials/first-web-api 19 Add Database support
  • 20. 20 Create a Models folder • There’s no ‘Models’ folder, so let’s make one • Right click on the project, then AddNew Folder: These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 21. 21 Add a Todo.cs to the Models folder namespace WebAPI_Demo.Models // This will be specific to you { public class TodoItem { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity) ] public long Key { get; set; } public string Name { get; set; } public bool IsComplete { get; set; {} } } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 22. 22 Examine code, Make sure it compiles • A Todo object represents a single row in the database • Looking at the code: • From https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api: • “The [Key] data annotation denotes the property, Key, is a unique identifier.” • “[DatabaseGenerated specifies the database will generate the key (rather than the application).” • “DatabaseGeneratedOption.Identity specifies the database should generate integer keys when a row is inserted.” • Build the program: Build  Build Solution These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 23. 23 Add a DbContext • From http://www.entityframeworktutorial.net/EntityFramework4.3/dbcontext- vs-objectcontext.aspx : • Handles all the translation between your objects and the actual database • For example, given a LINQ-to-Entity it will generate the SQL to query/add/update/delete rows in the database • Add the file • Right click on the ‘Models’ folder • Add Class • Name it TodoContext.cs (so it’s easy to find ) ☺ These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 24. 24 Add a DB context using Microsoft.EntityFrameworkCore; namespace WebAPI_Demo.Models { public class TodoContext : DbContext { public TodoContext(DbContextOptions<TodoContext> options) : base(options) { } public DbSet<TodoItem> TodoItems { get; set; } } } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 25. 25 How does TodoContext work? • It inherits from the DbContext, which does all the work public class TodoContext : DbContext { • DbContext must use magic reflection to inspect each object in order to be able to know how to map it to SQL and back • General description: Wikipedia • Warning: a bit dense – skim it to get the gist of it, but don’t worry about details • C#-specific docs These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 26. 26 Next step: add a repository class • First, this is a terrible re-use of the word repository • Second, this is implemented via dependency injection • What is Dependency Injection? • Let’s say that you’ve got two classes, Car and Engine. • Normally each Car object will create an Engine for itself, usually in the constructor • We can say that the Car object depends on (it uses) the Engine object • Instead of having the Car create an Engine object, we will give it an Engine to use. AKA, we will inject the Engine object into the Car, which the Car then uses (the Car then depends on). These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 27. 27 Next step: add a repository class • Key Benefit: We can now replace the Engine with other classes that also behave like Engines • Example: We could test our Car using a TestEngine which also logs the things it’s being told to do. We then check the log later to make sure the the Car was telling the Engine the correct things • Example: What if the Car object can save itself to a database? With DI, we could give it a different database object, and save itself into a different DB (for example, an Oracle DB instead of a MySQL) • Google for ‘dependency injection simple explanation’ • ‘Simple’ explanation #1 • ‘Simple’ explanation #2 These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 28. 28 Create the Repository class ( ITodoRepository.cs, in the Models folder) using System.Collections.Generic; namespace WebAPI_Demo.Models { public interface ITodoRepository { void Add(TodoItem item); IEnumerable<TodoItem> GetAll(); TodoItem Find(long key); void Remove(long key); void Update(TodoItem item); } } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 29. 29 Should we review interfaces here? ☺ These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 30. 30 TodoRepository.cs (also in Models folder) • Entire code is right above https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api#re gister-the-repository • From the same link: • “By defining a repository interface, we can decouple the repository class from the MVC controller that uses it. Instead of instantiating a TodoRepository inside the controller we will inject an ITodoRepository using the built-in support in ASP.NET Core for dependency injection.” These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 31. 31 Repository: Inheritance using System.Collections.Generic; using System.Linq; namespace WebAPI_Demo.Models { public class TodoRepository : ITodoRepository { private readonly TodoContext _context; public TodoRepository(TodoContext context) { _context = context; if (_context.TodoItems.Count() == 0) Add(new TodoItem { Name = "Item1" }); } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 32. 32 Implementing the ITodoRepository interface 1 / 2 public IEnumerable<TodoItem> GetAll() { return _context.TodoItems.ToList(); } public void Add(TodoItem item) { _context.TodoItems.Add(item); _context.SaveChanges(); } public TodoItem Find(long key) { return _context.TodoItems.FirstOrDefault(t => t.Key == key); } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 33. 33 Implementing the ITodoRepository interface 2 / 2 public void Remove(long key) { var entity = _context.TodoItems.First(t => t.Key == key); _context.TodoItems.Remove(entity); _context.SaveChanges(); } public void Update(TodoItem item) { _context.TodoItems.Update(item); _context.SaveChanges(); } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 34. 34 Register the repository • In Startup.cs, find the method named ConfigureServices() • Replace it with public void ConfigureServices(IServiceCollection services) { services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase()); services.AddMvc(); // already present services.AddScoped<ITodoRepository, TodoRepository>(); } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 35. 35 Register the repository • In Startup.cs, add the following to the top of the file: using WebAPI_Demo.Models; using Microsoft.EntityFrameworkCore; These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 36. 36 Dependency Injection • https://davidzych.com/dependency-injection-in-asp-net-vnext/ These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 37. These slides are mostly a walkthrough of https://docs.microsoft.com/en-us/as pnet/core/tutorials/first-web-api 37 Adding a controller
  • 38. 38 Add the controller file • Right click on the Controllers folder, Add  New Item • You want MVC Controller class. • Make sure it’s for .Net Core • Name it TodoController.cs These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 39. 39 I’m going to switch it up from here • Creating the slides takes a lot of time • And it’s all being copied from that tutorial page anyways • So instead of showing you everything, we’re going to try something new: •  Slides will be used as notes These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 40. 40 Copy from tutorial • From https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api #getting-to-do-items These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 41. 41 Controller - Routing • Routing can be done with attributes: [Route("api/[controller]")] public class TodoController : Controller • [controller] placeholder: • The class name minus the ‘Controller’ part • “TodoController” – “Controller” = “Todo” • URLs will start with <server info>/api/todo • NOTE: Routes are case INsensitive in ASP.Net Core – Todo, todo, ToDo, tOdO all work • Example: • http://localhost:51070/api/todo <server info>/api/[controller] These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 42. 42 Controller - Routing • More Details: • Attribute Routing Overall • Placeholders: The [controller] thing These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 43. 43 Routing details [HttpGet] // matches /api/[controller] public IEnumerable<TodoItem> GetAll() { return _todoRepository.GetAll(); } • Example: http://localhost:51070/api/todo - goes to GetAll • Example: http://localhost:51070/api/todo/3 - goes to GetById [HttpGet("{id}", Name = "GetTodo")] public IActionResult GetById(long id) { // ‘Name’ in HttpGet is for internal (code) purposes These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 44. 44 Run it • Control + F5, then wait for the browser to appear • Let’s play experiment with the browser results a bit • WARNING: when your browser window closes, the web server shuts down • Then let’s experiment with Postman for a bit • With the browser window still open ☺ These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 45. 45 Create • ‘CreatedAtRoute’ is a method that returns an object that will eventually become an HTTP 201 response • This includes a ‘Location’ field • Postman – POST • { "name":"walk dog", "isComplete":true } These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 46. 46 Update • Note – must send the entire object • Can use HTTP PATCH for just sending the delta • We’re not handling PATCH requests here • Postman • PUT request • http://localhost:51071/api/todo/2  2 is the id of the thing to update • Must include the entire object (including ‘key’, which is weird ) ☺ • { "key": "2", "name":"walk dog woooo it’s chaaaaaaanged!“, "isComplete":false } • 204 (No Content) is the ‘success’ response These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 47. 47 Delete • Postman – DELETE verb These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 48. 48 Debugging • Set a break point in Visual Studio • Launch the ASP.Net Core web app via Debug  Start Debugging • Have the browser (or Postman) make a request that will execute the breakpointed code • Visual Studio auto-magically stops where your breakpoint is! These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api
  • 49. 49 Where to go next? • Logging • Get information from your web app about what’s happening on the server • Critical for debugging • Can be set up to be viewed in Visual Studio • Client – JavaScipt • jQuery? • Angular? • React? • Vue? • Note: Frameworks are arranged in descending order of instructor familiarity ☺ These slides are mostly a walkthrough of https://docs.microsoft.com/en- us/aspnet/core/tutorials/first-web-api