Skip to content

Commit 73a5609

Browse files
author
Jennifer Deigendesch
committed
Added configuration
1 parent 1873191 commit 73a5609

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<li>Channel9: Creating REST API with ASP.NET Core (german): https://www.microsoft.com/germany/techwiese/know-how/video.aspx?id=msdn_de_67138</li>
55
<li>Managing Static Files in ASP.NET Core: https://www.youtube.com/watch?v=n3rL0ekYEOM</li>
66
<li>ASP.NET Web API Help Pages using Swagger: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger</li>
7+
<li>Middleware und Dependency Injection: https://channel9.msdn.com/Series/Einfhrung-in-ASPNET-Core/02-Middleware-und-Dependency-Injection</li>
78
</ul>

src/ASPNETCoreExample/Startup.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Logging;
1011
using Microsoft.Extensions.PlatformAbstractions;
@@ -19,6 +20,26 @@
1920
/// Start class of the API.
2021
/// </summary>
2122
public class Startup {
23+
/// <summary>
24+
/// Constructor.
25+
/// </summary>
26+
/// <param name="env"></param>
27+
public Startup(IHostingEnvironment env) {
28+
var builder =
29+
new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
30+
.AddJsonFile("appsettings.json")
31+
.AddEnvironmentVariables();
32+
33+
this.Configuration = builder.Build();
34+
}
35+
36+
/// <summary>
37+
/// App config.
38+
/// </summary>
39+
private IConfiguration Configuration {
40+
get;
41+
}
42+
2243
/// <summary>
2344
/// This method gets called by the runtime. Use this method to add services to the container.
2445
/// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
@@ -30,10 +51,9 @@ public void ConfigureServices(IServiceCollection services) {
3051

3152
// Inject an implementation of ISwaggerProvider with defaulted settings applied
3253
services.AddSwaggerGen();
33-
3454
services.ConfigureSwaggerGen(
3555
options => {
36-
options.SingleApiVersion(CreateSwaggerInfo());
56+
options.SingleApiVersion(this.CreateSwaggerInfo());
3757

3858
// Determine base path for the application.
3959
string basePath = PlatformServices.Default.Application.ApplicationBasePath;
@@ -54,6 +74,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5474
loggerFactory.AddConsole();
5575

5676
if (env.IsDevelopment()) {
77+
// Show info about an exception if one occurs only in development environment
5778
app.UseDeveloperExceptionPage();
5879
}
5980

@@ -65,10 +86,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6586

6687
InitMapper();
6788

68-
// Enable middleware to serve generated Swagger as a JSON endpoint
89+
// Enable middleware to use swagger ui
6990
app.UseSwagger();
70-
71-
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
7291
app.UseSwaggerUi();
7392
}
7493

@@ -87,16 +106,15 @@ private static void InitMapper() {
87106
});
88107
}
89108

90-
private static Info CreateSwaggerInfo() {
109+
private Info CreateSwaggerInfo() {
91110
return new Info {
92-
Version = "v1",
93-
Title = "ASP.NET Core v1.0 API",
94-
Description = "This is an example of a REST API with ASP.NET Core v1.0",
95-
TermsOfService = "None",
111+
Version = this.Configuration["version"],
112+
Title = this.Configuration["title"],
113+
Description = this.Configuration["description"],
96114
Contact = new Contact {
97-
Name = "Jennifer Deigendesch",
98-
Email = "[email protected]",
99-
Url = "https://github.com/doubleSlashde/ASPNETCoreExample"
115+
Name = this.Configuration["contact-name"],
116+
Email = this.Configuration["contact-email"],
117+
Url = this.Configuration["contact-url"]
100118
}
101119
};
102120
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": "v1",
3+
"title": "ASP.NET Core v1.0 API",
4+
"description": "This is an example of a REST API with ASP.NET Core v1.0",
5+
"contact-name": "Jennifer Deigendesch",
6+
"contact-email": "[email protected]",
7+
"contact-url": "https://github.com/doubleSlashde/ASPNETCoreExample"
8+
}

src/ASPNETCoreExample/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.1",
1414
"Microsoft.AspNetCore.Mvc": "1.0.1",
1515
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
16-
"Swashbuckle": "6.0.0-beta902"
16+
"Swashbuckle": "6.0.0-beta902",
17+
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
18+
"Microsoft.Extensions.Configuration.Json": "1.0.0"
1719
},
1820

1921
"tools": {

0 commit comments

Comments
 (0)