1
+ { {> partial_header} }
1
2
using System;
3
+ using System.Collections.Generic;
2
4
using System.IO;
3
5
using System.Linq;
4
- using Microsoft.AspNet.Builder;
5
- using Microsoft.AspNet.Hosting;
6
+ using System.Threading.Tasks;
7
+ using System.Xml.XPath;
8
+ using Microsoft.AspNetCore.Builder;
9
+ using Microsoft.AspNetCore.Hosting;
6
10
using Microsoft.Extensions.Configuration;
7
11
using Microsoft.Extensions.DependencyInjection;
8
12
using Microsoft.Extensions.Logging;
9
- using Microsoft.Extensions.PlatformAbstractions;
10
13
using Newtonsoft.Json.Serialization;
11
- using Swashbuckle.SwaggerGen ;
12
- using Swashbuckle.SwaggerGen.XmlComments ;
14
+ using Swashbuckle.Swagger.Model ;
15
+ using Swashbuckle.SwaggerGen.Annotations ;
13
16
14
17
namespace { {packageName} }
15
18
{
16
19
public class Startup
17
20
{
18
21
private readonly IHostingEnvironment _hostingEnv;
19
- private readonly IApplicationEnvironment _appEnv;
20
22
21
- public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
23
+ public IConfigurationRoot Configuration { get; }
24
+
25
+ public Startup(IHostingEnvironment env)
22
26
{
23
27
_hostingEnv = env;
24
- _appEnv = appEnv;
25
28
26
- // Set up configuration sources.
27
29
var builder = new ConfigurationBuilder()
28
- .AddJsonFile(" appsettings.json" )
30
+ .SetBasePath(env.ContentRootPath)
31
+ .AddJsonFile(" appsettings.json" , optional: true , reloadOnChange: true )
32
+ .AddJsonFile($" appsettings.{env.EnvironmentName}.json" , optional: true )
29
33
.AddEnvironmentVariables();
30
34
Configuration = builder.Build();
31
35
}
32
-
33
- public IConfigurationRoot Configuration { get; set; }
34
-
36
+
37
+
35
38
// This method gets called by the runtime. Use this method to add services to the container.
36
39
public void ConfigureServices(IServiceCollection services)
37
40
{
38
- string xmlComments = string.Format(@" {0}{4}artifacts{4}{1}{4}{2}{3}{4}{{packageName}}.xml" ,
39
- GetSolutionBasePath(),
40
- _appEnv.Configuration,
41
- _appEnv.RuntimeFramework.Identifier.ToLower(),
42
- _appEnv.RuntimeFramework.Version.ToString().Replace(" ." , string.Empty),
43
- Path.DirectorySeparatorChar);
44
-
45
41
// Add framework services.
46
42
services.AddMvc()
47
43
.AddJsonOptions(
48
44
opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } );
49
-
50
- // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
51
- // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
52
- // services.AddWebApiConventions();
53
-
45
+
54
46
services.AddSwaggerGen();
55
- services.ConfigureSwaggerDocument(options =>
47
+
48
+ services.ConfigureSwaggerGen(options =>
56
49
{
57
50
options.SingleApiVersion(new Info
58
51
{
59
52
Version = " v1" ,
60
53
Title = " {{packageName}}" ,
61
- Description = " {{packageName}} (ASP.NET 5 Web API 2.x )"
54
+ Description = " {{packageName}} (ASP.NET Core 1.0 )"
62
55
} );
63
-
64
- options.OperationFilter(new ApplyXmlActionCommentsFixed(xmlComments));
65
- });
66
56
67
- services.ConfigureSwaggerSchema(options => {
68
- options.DescribeAllEnumsAsStrings = true ;
69
- options.ModelFilter(new ApplyXmlTypeCommentsFixed(xmlComments));
57
+ options.DescribeAllEnumsAsStrings();
58
+
59
+ var comments = new XPathDocument($"{ AppContext.BaseDirectory} { Path.DirectorySeparatorChar} { _hostingEnv.ApplicationName} .xml");
60
+ options.OperationFilter<XmlCommentsOperationFilter >(comments);
61
+ options.ModelFilter<XmlCommentsModelFilter >(comments);
70
62
});
63
+
71
64
}
72
65
73
66
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
74
67
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
75
68
{
76
- loggerFactory.MinimumLevel = LogLevel.Information;
77
69
loggerFactory.AddConsole(Configuration.GetSection(" Logging" ));
78
70
loggerFactory.AddDebug();
79
71
80
- app.UseIISPlatformHandler ();
81
-
72
+ app.UseMvc ();
73
+
82
74
app.UseDefaultFiles();
83
75
app.UseStaticFiles();
84
76
85
- app.UseMvc();
86
-
87
- app.UseSwaggerGen();
77
+ app.UseSwagger();
88
78
app.UseSwaggerUi();
89
79
}
90
-
91
- // Taken from https://github.com/domaindrivendev/Ahoy/blob/master/test/WebSites/Basic/Startup.cs
92
- private string GetSolutionBasePath()
93
- {
94
- var dir = Directory.CreateDirectory(_appEnv.ApplicationBasePath);
95
- while (dir.Parent != null)
96
- {
97
- if (dir.GetDirectories(" artifacts" ).Any())
98
- return dir.FullName;
99
-
100
- dir = dir.Parent;
101
- }
102
- throw new InvalidOperationException("Failed to detect solution base path - artifacts not found. Did you run dnu pack --out artifacts?");
103
- }
104
-
105
- // Entry point for the application.
106
- public static void Main(string[] args) => WebApplication.Run<Startup >(args);
107
- }
108
-
109
-
110
- // using Swashbuckle.SwaggerGen.XmlComments;
111
- public class ApplyXmlTypeCommentsFixed : ApplyXmlTypeComments
112
- {
113
- public ApplyXmlTypeCommentsFixed() : base(" " )
114
- {
115
- throw new NotImplementedException();
116
- }
117
-
118
- public ApplyXmlTypeCommentsFixed(string filePath): base(filePath)
119
- {
120
-
121
- }
122
- }
123
-
124
- public class ApplyXmlActionCommentsFixed : ApplyXmlActionComments
125
- {
126
- public ApplyXmlActionCommentsFixed() : base(" " )
127
- {
128
- throw new NotImplementedException();
129
- }
130
-
131
- public ApplyXmlActionCommentsFixed(string filePath): base(filePath)
132
- {
133
-
134
- }
135
80
}
136
- }
81
+ }
0 commit comments