Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
597e944
Updated first-web-api tutorial (#34558)
Feb 8, 2025
e5b3610
Additional samples, ddoc and screenshot update to lukekaese's PR
wadepickett Feb 13, 2025
656b668
Merge branch 'lukekaese-pr-update-wpickett' into main
wadepickett Feb 13, 2025
26ebc0b
Update first-web-api.md
Feb 14, 2025
7259e72
Remove redundant code snippet from tutorial
wadepickett Feb 14, 2025
62c3551
Fix code snippet reference in tutorial
wadepickett Feb 14, 2025
cde49de
Fix code snippet link in tutorial: removed typo
wadepickett Feb 14, 2025
ab04928
Add bullet points to action instructions in tutorial
wadepickett Feb 15, 2025
5484522
Apply suggestions from code review
wadepickett Feb 15, 2025
82a57dd
Adding more of tdykstra's review suggestions
wadepickett Feb 15, 2025
08f5b50
Fix port number placeholder in tutorial
wadepickett Feb 15, 2025
5d29651
Clarify OpenAPI and Swagger UI port details
wadepickett Feb 15, 2025
ed21ec8
Fix list: Add missing line break in tutorial
wadepickett Feb 15, 2025
d898d3e
Fix list formatting in tutorial markdown
wadepickett Feb 15, 2025
eb62cda
Update instructions for running and viewing output
wadepickett Feb 16, 2025
b7722ca
Update tutorial with Swagger UI configuration steps
wadepickett Feb 18, 2025
1988ba9
Moved project template details in tutorial to just after creation
wadepickett Feb 18, 2025
941c0c7
Fix formatting for request URL instructions
wadepickett Feb 18, 2025
e5938c1
Fix list formatting for packages
wadepickett Feb 19, 2025
2f8bc20
Format NuGet package names as code, isolating list issue
wadepickett Feb 19, 2025
89c91c1
Fix markdown header levels in tutorial
wadepickett Feb 19, 2025
0b12d08
Update formatting for stop button instructions
wadepickett Feb 19, 2025
90910ef
Fix indentation in first-web-api tutorial
wadepickett Feb 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 145 additions & 82 deletions aspnetcore/tutorials/first-web-api.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TodoApi.Models;

namespace TodoApi.Controllers
{
// <snippet_Route>
[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
// </snippet_Route>
{
private readonly TodoContext _context;

Expand Down Expand Up @@ -40,7 +47,7 @@ public async Task<ActionResult<TodoItem>> GetTodoItem(long id)

// PUT: api/TodoItems/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
// <snippet_Update>
// <snippet_PutTodoItem>
[HttpPut("{id}")]
public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)
{
Expand Down Expand Up @@ -69,7 +76,7 @@ public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)

return NoContent();
}
// </snippet_Update>
// </snippet_PutTodoItem>

// POST: api/TodoItems
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
Expand All @@ -86,6 +93,7 @@ public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
// </snippet_Create>

// DELETE: api/TodoItems/5
// <snippet_Delete>
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteTodoItem(long id)
{
Expand All @@ -100,6 +108,7 @@ public async Task<IActionResult> DeleteTodoItem(long id)

return NoContent();
}
// </snippet_Delete>

private bool TodoItemExists(long id)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace TodoApi.Models;

Expand All @@ -10,4 +10,4 @@ public TodoContext(DbContextOptions<TodoContext> options)
}

public DbSet<TodoItem> TodoItems { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace TodoApi.Models;
namespace TodoApi.Models;

public class TodoItem
{
public long Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
}
}
54 changes: 50 additions & 4 deletions aspnetcore/tutorials/first-web-api/samples/9.0/TodoApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
#define Final_Add_DBContext // First_Add_SwaggerUI
#if First_Add_SwaggerUI
// <snippet_First_Add_SwaggerUI>
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();

app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/openapi/v1.json", "v1");
});
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
// </snippet_First_Add_SwaggerUI>
#endif

#if Final_Add_DBContext
// <snippet_Final_Add_DBContext>
using Microsoft.EntityFrameworkCore;
using TodoApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddDbContext<TodoContext>(opt =>
opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();

app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/openapi/v1.json", "v1");
});
}

app.UseHttpsRedirection();
Expand All @@ -24,3 +68,5 @@
app.MapControllers();

app.Run();
// </snippet_Final_Add_DBContext>
#endif
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>

<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-rc.2.22476.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.2.22472.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.2.22472.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0-rc.2.22472.11">
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0-rc.2.22510.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public class WeatherForecast

public string? Summary { get; set; }
}
}
}
Loading