Skip to content

Commit 6182816

Browse files
committed
Added ASP.NET Core example
1 parent 5188764 commit 6182816

52 files changed

Lines changed: 2153 additions & 35 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.vscode/launch.json‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,23 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"name": "Launch SpotifyAPI.Web.Examples.ASP",
8+
"name": "Launch Example.ASP",
99
"type": "coreclr",
1010
"request": "launch",
11-
"preLaunchTask": "build-web-example-asp",
12-
// If you have changed target frameworks, make sure to update the program path.
13-
"program": "${workspaceFolder}/SpotifyAPI.Web.Examples.ASP/bin/Debug/netcoreapp3.0/SpotifyAPI.Web.Examples.ASP.dll",
11+
"preLaunchTask": "Example.ASP-build",
12+
"program": "${workspaceFolder}/SpotifyAPI.Web.Examples/Example.ASP/bin/Debug/netcoreapp3.1/Example.ASP.dll",
1413
"args": [],
15-
"cwd": "${workspaceFolder}/SpotifyAPI.Web.Examples.ASP",
14+
"cwd": "${workspaceFolder}/SpotifyAPI.Web.Examples/Example.ASP",
1615
"stopAtEntry": false,
17-
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
18-
"serverReadyAction": {
19-
"action": "openExternally",
20-
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
21-
},
16+
// "serverReadyAction": {
17+
// "pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
18+
// },
2219
"env": {
2320
"ASPNETCORE_ENVIRONMENT": "Development"
2421
},
2522
"sourceFileMap": {
2623
"/Views": "${workspaceFolder}/Views"
2724
}
28-
},
29-
{
30-
"name": "Launch SpotifyAPI.Web.Examples.CLI",
31-
"type": "coreclr",
32-
"request": "launch",
33-
"preLaunchTask": "build-web-example-cli",
34-
"program": "${workspaceFolder}/SpotifyAPI.Web.Examples.CLI/bin/Debug/netcoreapp3.0/SpotifyAPI.Web.Examples.CLI.dll",
35-
"args": [],
36-
"cwd": "${workspaceFolder}/SpotifyAPI.Web.Examples.CLI",
37-
"stopAtEntry": false
3825
}
3926
]
4027
}

‎.vscode/tasks.json‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@
22
"version": "2.0.0",
33
"tasks": [
44
{
5-
"label": "build-web-example-asp",
5+
"label": "Example.ASP-build",
66
"command": "dotnet",
77
"type": "process",
88
"args": [
99
"build",
10-
"${workspaceFolder}/SpotifyAPI.Web.Examples.ASP/SpotifyAPI.Web.Examples.ASP.csproj",
10+
"${workspaceFolder}/SpotifyAPI.Web.Examples/Example.ASP/Example.ASP.csproj",
1111
"/property:GenerateFullPaths=true",
1212
"/consoleloggerparameters:NoSummary"
1313
],
1414
"problemMatcher": "$msCompile"
15-
},
16-
{
17-
"label": "build-web-example-cli",
18-
"command": "dotnet",
19-
"type": "process",
20-
"args": [
21-
"build",
22-
"${workspaceFolder}/SpotifyAPI.Web.Examples.CLI/SpotifyAPI.Web.Examples.CLI.csproj",
23-
"/property:GenerateFullPaths=true",
24-
"/consoleloggerparameters:NoSummary"
25-
],
26-
"problemMatcher": "$msCompile"
27-
},
15+
}
2816
]
2917
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk.Web">
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<UserSecretsId>da29eac4-4c22-4a7f-b393-379e83b60998</UserSecretsId>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="AspNet.Security.OAuth.Spotify" Version="3.1.1" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
@if (Model.ShowRequestId)
11+
{
12+
<p>
13+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
14+
</p>
15+
}
16+
17+
<h3>Development Mode</h3>
18+
<p>
19+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20+
</p>
21+
<p>
22+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23+
It can result in displaying sensitive information from exceptions to end users.
24+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
25+
and restarting the app.
26+
</p>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Mvc.RazorPages;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Example.ASP.Pages
11+
{
12+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
13+
public class ErrorModel : PageModel
14+
{
15+
public string RequestId { get; set; }
16+
17+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
18+
19+
public ErrorModel() { }
20+
21+
public void OnGet()
22+
{
23+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
24+
}
25+
}
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@page
2+
@model IndexModel
3+
@inject SpotifyClientBuilder spotifyBuilder
4+
@{
5+
ViewData["Title"] = "Home";
6+
ViewData["Page"] = "index";
7+
}
8+
9+
<div class="text-center">
10+
<h1 class="display-4">
11+
Welcome,
12+
</h1>
13+
14+
<table class="pure-table">
15+
<thead>
16+
<tr>
17+
<th>Image</th>
18+
<th>Name</th>
19+
</tr>
20+
</thead>
21+
<tbody>
22+
@{
23+
foreach (var item in Model.Playlists.Items)
24+
{
25+
<tr>
26+
<td>
27+
@if(item.Images.Any()) {
28+
<img width="100" height="100" src="@item.Images[0].Url">
29+
}
30+
</td>
31+
<td>
32+
@item.Name
33+
</td>
34+
</tr>
35+
}
36+
}
37+
</tbody>
38+
</table>
39+
@if(Model.Previous != null) {
40+
<a href="@Model.Previous">Previous</a>
41+
}
42+
@if(Model.Next != null) {
43+
<a href="@Model.Next">Next</a>
44+
}
45+
</div>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
using SpotifyAPI.Web;
5+
6+
namespace Example.ASP.Pages
7+
{
8+
public class IndexModel : PageModel
9+
{
10+
private const int LIMIT = 10;
11+
private readonly SpotifyClientBuilder _spotifyClientBuilder;
12+
13+
public Paging<SimplePlaylist> Playlists { get; set; }
14+
15+
public string Next { get; set; }
16+
public string Previous { get; set; }
17+
18+
public IndexModel(SpotifyClientBuilder spotifyClientBuilder)
19+
{
20+
_spotifyClientBuilder = spotifyClientBuilder;
21+
}
22+
23+
public async Task OnGet()
24+
{
25+
var spotify = await _spotifyClientBuilder.BuildClient();
26+
27+
int offset = int.TryParse(Request.Query["Offset"], out offset) ? offset : 0;
28+
var playlistRequest = new PlaylistCurrentUsersRequest
29+
{
30+
Limit = LIMIT,
31+
Offset = offset
32+
};
33+
Playlists = await spotify.Playlists.CurrentUsers(playlistRequest);
34+
35+
if (Playlists.Next != null)
36+
{
37+
Next = Url.Page("Index", new { Offset = offset + LIMIT });
38+
}
39+
if (Playlists.Previous != null)
40+
{
41+
Previous = Url.Page("Index", values: new { Offset = offset - LIMIT });
42+
}
43+
}
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.AspNetCore.Mvc.RazorPages;
5+
using Microsoft.Extensions.Logging;
6+
using SpotifyAPI.Web;
7+
8+
namespace Example.ASP.Pages
9+
{
10+
public class ProfileModel : PageModel
11+
{
12+
private readonly SpotifyClientBuilder _spotifyClientBuilder;
13+
public ProfileModel(SpotifyClientBuilder spotifyClientBuilder)
14+
{
15+
_spotifyClientBuilder = spotifyClientBuilder;
16+
}
17+
18+
public PrivateUser Me { get; set; }
19+
20+
public async Task OnGet()
21+
{
22+
var spotify = await _spotifyClientBuilder.BuildClient();
23+
24+
Me = await spotify.UserProfile.Current();
25+
}
26+
27+
public async Task<IActionResult> OnPost()
28+
{
29+
await HttpContext.SignOutAsync();
30+
return Redirect("https://google.com");
31+
}
32+
}
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@page
2+
@model ProfileModel
3+
@{
4+
ViewData["Title"] = "Profile";
5+
ViewData["Page"] = "profile";
6+
ViewData["Description"] = "Have a look at your spotify info and logout of your account";
7+
}
8+
9+
<table class="pure-table">
10+
<thead>
11+
<th>Key</th>
12+
<th>Value</th>
13+
</thead>
14+
<tbody>
15+
<tr>
16+
<td>Id</td>
17+
<td>@Model.Me.Id</td>
18+
</tr>
19+
<tr>
20+
<td>Uri</td>
21+
<td>@Model.Me.Uri</td>
22+
</tr>
23+
<tr>
24+
<td>DisplayName</td>
25+
<td>@Model.Me.DisplayName</td>
26+
</tr>
27+
<tr>
28+
<td>Country</td>
29+
<td>@Model.Me.Country</td>
30+
</tr>
31+
<tr>
32+
<td>Product</td>
33+
<td>@Model.Me.Product</td>
34+
</tr>
35+
<tr>
36+
<td>E-Mail (unverfied)</td>
37+
<td>@Model.Me.Email</td>
38+
</tr>
39+
</tbody>
40+
</table>
41+
42+
<form method="post" asp-controller="Profile">
43+
<input type="submit" value="Logout"/>
44+
</form>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>@ViewData["Title"] - Example.ASP</title>
7+
<link
8+
rel="stylesheet"
9+
href="https://unpkg.com/purecss@2.0.3/build/pure-min.css"
10+
integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ"
11+
crossorigin="anonymous">
12+
<link rel="stylesheet" href="~/css/site.css" />
13+
</head>
14+
<body>
15+
<div id="layout">
16+
<!-- Menu toggle -->
17+
<a href="#menu" id="menuLink" class="menu-link">
18+
<!-- Hamburger icon -->
19+
<span></span>
20+
</a>
21+
22+
<div id="menu">
23+
<div class="pure-menu">
24+
<a class="pure-menu-heading" href="#">SpotifyAPI-NET ASP</a>
25+
26+
<ul class="pure-menu-list">
27+
<li class="pure-menu-item @((string)ViewData["Page"] == "index" ? "pure-menu-selected" : "")">
28+
<a class="pure-menu-link" asp-page="Index">Home</a>
29+
</li>
30+
<li class="pure-menu-item @((string)ViewData["Page"] == "profile" ? "pure-menu-selected" : "")">
31+
<a class="pure-menu-link" asp-page="Profile">Profile</a>
32+
</li>
33+
</ul>
34+
</div>
35+
</div>
36+
37+
<div id="main">
38+
<div class="header">
39+
<h1>@ViewData["Title"]</h1>
40+
@if (ViewData["Description"] != null)
41+
{
42+
<h2>@ViewData["Description"]</h2>
43+
}
44+
</div>
45+
46+
<div class="content">
47+
<main role="main" class="pb-3">
48+
@RenderBody()
49+
</main>
50+
</div>
51+
</div>
52+
</div>
53+
54+
<script src="~/js/site.js" asp-append-version="true"></script>
55+
56+
@RenderSection("Scripts", required: false)
57+
</body>
58+
</html>

0 commit comments

Comments
 (0)