From 532ed16bbd0f31129229aeee1faed72d49daf268 Mon Sep 17 00:00:00 2001 From: "chris.watts90@outlook.com" Date: Sun, 29 Jan 2017 16:09:26 +0000 Subject: [PATCH] Added package DalSoft.WebApi.HelpPage which will create help pages for every ApiController in the solution. --- .../DisplayTemplates/ApiGroup.cshtml | 28 ++++ .../DisplayTemplates/HelpPageApiModel.cshtml | 49 +++++++ .../DisplayTemplates/Parameters.cshtml | 55 +++++++ .../DisplayTemplates/Samples.cshtml | 37 +++++ .../HelpPage.css | 134 ++++++++++++++++++ .../DalSoft.WebApi.HelpPage.Views/api.cshtml | 28 ++++ .../index.cshtml | 43 ++++++ .../WindowsDataCenter/Service1.cs | 6 + .../WindowsDataCenter.csproj | 34 +++++ .../WindowsDataCenter/packages.config | 3 + .../WindowsDataServiceHost.csproj | 28 ++++ .../WindowsDataServiceHost/packages.config | 7 + 12 files changed, 452 insertions(+) create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/ApiGroup.cshtml create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/HelpPageApiModel.cshtml create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Parameters.cshtml create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Samples.cshtml create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/HelpPage.css create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/api.cshtml create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/index.cshtml diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/ApiGroup.cshtml b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/ApiGroup.cshtml new file mode 100644 index 0000000..7cfdde8 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/ApiGroup.cshtml @@ -0,0 +1,28 @@ +@using System.Web.Http.Description +@using DalSoft.WebApi.HelpPage +@model IGrouping + +

@Model.Key

+ + + + + + @foreach (var api in Model) + { + + + + + } + +
APIDescription
@api.HttpMethod.Method @api.RelativePath + @if (api.Documentation !=null) + { +

@api.Documentation

+ } + else + { +

No documentation available.

+ } +
\ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/HelpPageApiModel.cshtml b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/HelpPageApiModel.cshtml new file mode 100644 index 0000000..37bb2eb --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/HelpPageApiModel.cshtml @@ -0,0 +1,49 @@ +@using System.Collections.Generic +@using System.Net.Http.Headers +@using DalSoft.WebApi.HelpPage.Models + +@model HelpPageApiModel + +@{ + var description = Model.ApiDescription; + var hasParameters = description.ParameterDescriptions.Count > 0; + var hasRequestSamples = Model.SampleRequests.Count > 0; + var hasResponseSamples = Model.SampleResponses.Count > 0; +} +

@description.HttpMethod.Method @description.RelativePath

+
+ @{ + if (description.Documentation != null) + { +

@description.Documentation

+ } + else + { +

No documentation available.

+ } + + if (hasParameters || hasRequestSamples) + { +

Request Information

+ if (hasParameters) + { +

Parameters

+ @Include("Parameters.cshtml", Model, typeof(HelpPageApiModel)) + } + if (hasRequestSamples) + { +

Request body formats

+ var ModelSamples = Model.SampleRequests; + @Include("Samples.cshtml", ModelSamples, typeof(IDictionary)) + } + } + + if (hasResponseSamples) + { +

Response Information

+

Response body formats

+ var ModelSamples = Model.SampleResponses; + @Include("Samples.cshtml", ModelSamples, typeof(IDictionary)) + } +} +
\ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Parameters.cshtml b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Parameters.cshtml new file mode 100644 index 0000000..1a45b7c --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Parameters.cshtml @@ -0,0 +1,55 @@ +@using System.Collections.ObjectModel +@using System.Threading +@using System.Web.Http.Description + +@{ Collection parameters = Model.ApiDescription.ParameterDescriptions; } +@if (parameters.Count > 0) +{ + + + + + + @{ + foreach (ApiParameterDescription parameter in parameters) + { + var parameterDocumentation = parameter.Documentation ?? "No documentation available."; + + // Don't show CancellationToken because it's a special parameter + if (!typeof (CancellationToken).IsAssignableFrom(parameter.ParameterDescriptor.ParameterType)) + { + + + + + + } + } + } + +
NameDescriptionAdditional information
@parameter.Name +

@parameterDocumentation

+
+ @{ + switch (parameter.Source) + { + case ApiParameterSource.FromBody: +

Define this parameter in the request body. +

+ break; + case ApiParameterSource.FromUri: +

Define this parameter in the request URI. +

+ break; + case ApiParameterSource.Unknown: + default: +

None.

+ break; + } + } +
+} +else +{ +

None.

+} \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Samples.cshtml b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Samples.cshtml new file mode 100644 index 0000000..c76f82d --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/DisplayTemplates/Samples.cshtml @@ -0,0 +1,37 @@ +@using System.Net.Http.Headers +@using DalSoft.WebApi.HelpPage.SampleGeneration +@model IDictionary + +@{ + // Group the samples into a single tab if they are the same. + var samples = Model.GroupBy(x => x.Value).ToDictionary(x => string.Join(", ", x.Select(m => m.Key.ToString()).ToArray()), x => x.Key); + var mediaTypes = samples.Keys; +} +
+ @{ + foreach (var mediaType in mediaTypes) + { +

@mediaType

+
+ Sample: + @{ var sample = samples[mediaType]; } + @if (sample == null) + { +

Sample not available.

+ } + else if (sample is TextSample) + { +
@(((TextSample)sample).Text)
+ } + else if (sample is ImageSample) + { + + } + else if (sample is InvalidSample) + { +
@(((InvalidSample)sample).ErrorMessage)
+ } +
+ } + } +
\ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/HelpPage.css b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/HelpPage.css new file mode 100644 index 0000000..aff2230 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/HelpPage.css @@ -0,0 +1,134 @@ +.help-page h1, +.help-page .h1, +.help-page h2, +.help-page .h2, +.help-page h3, +.help-page .h3, +#body.help-page, +.help-page-table th, +.help-page-table pre, +.help-page-table p { + font-family: "Segoe UI Light", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif; +} + +.help-page pre.wrapped { + white-space: -moz-pre-wrap; + white-space: -pre-wrap; + white-space: -o-pre-wrap; + white-space: pre-wrap; +} + +.help-page .warning-message-container { + margin-top: 20px; + padding: 0 10px; + color: #525252; + background: #EFDCA9; + border: 1px solid #CCCCCC; +} + +.help-page-table { + width: 100%; + border-collapse: collapse; + text-align: left; + margin: 0px 0px 20px 0px; + border-top: 1px solid #D4D4D4; +} + +.help-page-table th { + text-align: left; + font-weight: bold; + border-bottom: 1px solid #D4D4D4; + padding: 5px 6px 5px 6px; +} + +.help-page-table td { + border-bottom: 1px solid #D4D4D4; + padding: 10px 8px 10px 8px; + vertical-align: top; +} + +.help-page-table pre, +.help-page-table p { + margin: 0px; + padding: 0px; + font-family: inherit; + font-size: 100%; +} + +.help-page-table tbody tr:hover td { + background-color: #F3F3F3; +} + +.help-page a:hover { + background-color: transparent; +} + +.help-page .sample-header { + border: 2px solid #D4D4D4; + background: #00497E; + color: #FFFFFF; + padding: 8px 15px; + border-bottom: none; + display: inline-block; + margin: 10px 0px 0px 0px; +} + +.help-page .sample-content { + display: block; + border-width: 0; + padding: 15px 20px; + background: #FFFFFF; + border: 2px solid #D4D4D4; + margin: 0px 0px 10px 0px; +} + +.help-page .api-name { + width: 40%; +} + +.help-page .api-documentation { + width: 60%; +} + +.help-page .parameter-name { + width: 20%; +} + +.help-page .parameter-documentation { + width: 40%; +} + +.help-page .parameter-type { + width: 20%; +} + +.help-page .parameter-annotations { + width: 20%; +} + +.help-page h1, +.help-page .h1 { + font-size: 36px; + line-height: normal; +} + +.help-page h2, +.help-page .h2 { + font-size: 24px; +} + +.help-page h3, +.help-page .h3 { + font-size: 20px; +} + +#body.help-page { + font-size: 14px; + line-height: 143%; + color: #333; +} + +.help-page a { + color: #0000EE; + text-decoration: none; +} diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/api.cshtml b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/api.cshtml new file mode 100644 index 0000000..8c58cd7 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/api.cshtml @@ -0,0 +1,28 @@ +@using DalSoft.WebApi.HelpPage.Models +@model HelpPageApiModel + +@{ + var description = Model.ApiDescription; + var title = description.HttpMethod.Method + " " + description.RelativePath; +} + + + + @title + + + +
+ +
+ @Include("HelpPageApiModel.cshtml", Model, typeof(HelpPageApiModel)) +
+
+ + diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/index.cshtml b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/index.cshtml new file mode 100644 index 0000000..e4ccb5b --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Help/DalSoft.WebApi.HelpPage.Views/index.cshtml @@ -0,0 +1,43 @@ +@using System.Web.Http.Description +@using System.Collections.ObjectModel +@using System.Linq +@model Collection + +@{ + ViewBag.Title = "ASP.NET Web API Help Page"; + + // Group APIs by controller + ILookup apiGroups = Model.ToLookup(api => api.ActionDescriptor.ControllerDescriptor.ControllerName); +} + + + + @ViewBag.Title + + + +
+
+
+

@ViewBag.Title

+
+
+
+
+ +
+ @foreach (IGrouping controllerGroup in apiGroups) + { + @Include("ApiGroup.cshtml", controllerGroup, typeof (IGrouping)) + } +
+
+ + diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs index 807a109..f1375da 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs @@ -11,6 +11,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web.Http; +using DalSoft.WebApi.HelpPage; using Microsoft.Owin.FileSystems; using Microsoft.Owin.Hosting; using Microsoft.Owin.StaticFiles; @@ -84,6 +85,11 @@ namespace WindowsDataCenter appBuilder.UseWebApi(config); +#if DEBUG + //Add the help-pages extension only in Debug mode. + appBuilder.UseWebApiHelpPage(config, "help-pages"); +#endif + var fileSystem = new PhysicalFileSystem(@"./www"); var options = new FileServerOptions { diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj index 60b6e87..b167aef 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + bin\Debug\WindowsDataCenter.XML AnyCPU @@ -33,6 +34,10 @@ 4 + + ..\packages\DalSoft.WebApi.HelpPage.0.0.7.0\lib\net451\DalSoft.WebApi.HelpPage.dll + True + ..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll True @@ -61,6 +66,10 @@ ..\packages\Owin.1.0\lib\net40\Owin.dll True + + ..\packages\RazorEngine.3.7.2\lib\net45\RazorEngine.dll + True + @@ -80,6 +89,10 @@ ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll True + + ..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll + True + @@ -102,6 +115,24 @@ + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + PreserveNewest @@ -123,6 +154,9 @@ + + PreserveNewest + PreserveNewest diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config index 39840fb..86aaa9b 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config @@ -1,5 +1,7 @@  + + @@ -12,4 +14,5 @@ + \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/WindowsDataServiceHost.csproj b/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/WindowsDataServiceHost.csproj index 726578d..af3b519 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/WindowsDataServiceHost.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/WindowsDataServiceHost.csproj @@ -33,13 +33,41 @@ 4 + + ..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll + True + ..\packages\Microsoft.Owin.Host.HttpListener.3.0.1\lib\net45\Microsoft.Owin.Host.HttpListener.dll True + + ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll + True + + + ..\packages\Owin.1.0\lib\net40\Owin.dll + True + + + ..\packages\RazorEngine.3.7.2\lib\net45\RazorEngine.dll + True + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + True + + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll + True + + + ..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll + True + diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/packages.config b/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/packages.config index 375a96f..f7f4e34 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/packages.config +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataServiceHost/packages.config @@ -1,4 +1,11 @@  + + + + + + + \ No newline at end of file