Skip to content

Commit 49f30a7

Browse files
authored
Merge pull request #6 from sakapon-net/v2.0
v2.0.6
2 parents b28c661 + f05dabe commit 49f30a7

File tree

101 files changed

+4746
-1603
lines changed

Some content is hidden

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

101 files changed

+4746
-1603
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
11
# Random Data Web API
2-
Provides the JSON/JSONP API to generate random data.
2+
Provides the JSON Web API to generate random data.
3+
This Web API supports CORS (Cross-Origin Resource Sharing).
4+
5+
Using PaaS is the simplest way to host this Web API.
6+
For example, if you fork this repository, you can deploy directly the Web API to an Azure Web App by the Microsoft Azure Portal.
7+
In this case, the continuous deployment is configured.
8+
9+
[日本語のドキュメント](docs)
10+
11+
## Random Data
12+
- alphabets
13+
- alphanumerics
14+
- byte sequence
15+
- UUID (GUID)
16+
- time-ordered ID
17+
18+
## Web App
19+
This project is actually the ASP.NET Web app that contains the following:
20+
- Web API
21+
- help page with specification
22+
- test page using jQuery
23+
24+
[randomdata.azurewebsites.net](https://randomdata.azurewebsites.net/) is a sample deployment.
25+
26+
### Development Environment
27+
- .NET Framework 4.5
28+
- ASP.NET Web API 5.2.3
29+
- ASP.NET Web API Help Page 5.2.3
30+
- ASP.NET Web API Cross-Origin Support 5.2.3
31+
- [Blaze 1.1.10](https://github.com/sakapon/Blaze)
32+
33+
### Release Notes
34+
- **v1.0.0** The first release, using ASP.NET MVC.
35+
- **v2.0.6** Use ASP.NET Web API.

RandomData2/RandomData2.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27428.2015
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomDataWebApi", "RandomDataWebApi\RandomDataWebApi.csproj", "{A62DF1CA-11D0-427E-91CB-C08281D3DE7C}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "UnitTest\UnitTest.csproj", "{43896562-E647-4F9E-8498-8542FCA26834}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{A62DF1CA-11D0-427E-91CB-C08281D3DE7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{A62DF1CA-11D0-427E-91CB-C08281D3DE7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{A62DF1CA-11D0-427E-91CB-C08281D3DE7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{A62DF1CA-11D0-427E-91CB-C08281D3DE7C}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{43896562-E647-4F9E-8498-8542FCA26834}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{43896562-E647-4F9E-8498-8542FCA26834}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{43896562-E647-4F9E-8498-8542FCA26834}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{43896562-E647-4F9E-8498-8542FCA26834}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {724388ED-9668-4465-8412-8C3C76730BC3}
30+
EndGlobalSection
31+
EndGlobal

RandomGenerator/RandomWebApp/App_Start/WebApiConfig.cs renamed to RandomData2/RandomDataWebApi/App_Start/WebApiConfig.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Web.Http;
5+
using System.Web.Http.Cors;
56

6-
namespace RandomWebApp
7+
namespace RandomDataWebApi
78
{
89
public static class WebApiConfig
910
{
1011
public static void Register(HttpConfiguration config)
1112
{
13+
// Web API の設定およびサービス
14+
15+
config.Formatters.Remove(config.Formatters.XmlFormatter);
16+
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
17+
18+
// Web API ルート
19+
config.MapHttpAttributeRoutes();
20+
1221
config.Routes.MapHttpRoute(
1322
name: "DefaultApi",
1423
routeTemplate: "api/{controller}/{id}",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Text;
3+
using System.Web;
4+
using System.Web.Http.Description;
5+
6+
namespace RandomDataWebApi.Areas.HelpPage
7+
{
8+
public static class ApiDescriptionExtensions
9+
{
10+
/// <summary>
11+
/// Generates an URI-friendly ID for the <see cref="ApiDescription"/>. E.g. "Get-Values-id_name" instead of "GetValues/{id}?name={name}"
12+
/// </summary>
13+
/// <param name="description">The <see cref="ApiDescription"/>.</param>
14+
/// <returns>The ID as a string.</returns>
15+
public static string GetFriendlyId(this ApiDescription description)
16+
{
17+
string path = description.RelativePath;
18+
string[] urlParts = path.Split('?');
19+
string localPath = urlParts[0];
20+
string queryKeyString = null;
21+
if (urlParts.Length > 1)
22+
{
23+
string query = urlParts[1];
24+
string[] queryKeys = HttpUtility.ParseQueryString(query).AllKeys;
25+
queryKeyString = String.Join("_", queryKeys);
26+
}
27+
28+
StringBuilder friendlyPath = new StringBuilder();
29+
friendlyPath.AppendFormat("{0}-{1}",
30+
description.HttpMethod.Method,
31+
localPath.Replace("/", "-").Replace("{", String.Empty).Replace("}", String.Empty));
32+
if (queryKeyString != null)
33+
{
34+
friendlyPath.AppendFormat("_{0}", queryKeyString.Replace('.', '-'));
35+
}
36+
return friendlyPath.ToString();
37+
}
38+
}
39+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Uncomment the following to provide samples for PageResult<T>. Must also add the Microsoft.AspNet.WebApi.OData
2+
// package to your project.
3+
////#define Handle_PageResultOfT
4+
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
10+
using System.Linq;
11+
using System.Net.Http.Headers;
12+
using System.Reflection;
13+
using System.Web;
14+
using System.Web.Http;
15+
#if Handle_PageResultOfT
16+
using System.Web.Http.OData;
17+
#endif
18+
19+
namespace RandomDataWebApi.Areas.HelpPage
20+
{
21+
/// <summary>
22+
/// Use this class to customize the Help Page.
23+
/// For example you can set a custom <see cref="System.Web.Http.Description.IDocumentationProvider"/> to supply the documentation
24+
/// or you can provide the samples for the requests/responses.
25+
/// </summary>
26+
public static class HelpPageConfig
27+
{
28+
[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
29+
MessageId = "RandomDataWebApi.Areas.HelpPage.TextSample.#ctor(System.String)",
30+
Justification = "End users may choose to merge this string with existing localized resources.")]
31+
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly",
32+
MessageId = "bsonspec",
33+
Justification = "Part of a URI.")]
34+
public static void Register(HttpConfiguration config)
35+
{
36+
// Uncomment the following to use the documentation from XML documentation file.
37+
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/RandomDataWebApi.xml")));
38+
39+
// Uncomment the following to use "sample string" as the sample for all actions that have string as the body parameter or return type.
40+
// Also, the string arrays will be used for IEnumerable<string>. The sample objects will be serialized into different media type
41+
// formats by the available formatters.
42+
config.SetSampleObjects(new Dictionary<Type, object>
43+
{
44+
{ typeof(int), 8 },
45+
// {typeof(string), "sample string"},
46+
// {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
47+
});
48+
49+
// Extend the following to provide factories for types not handled automatically (those lacking parameterless
50+
// constructors) or for which you prefer to use non-default property values. Line below provides a fallback
51+
// since automatic handling will fail and GeneratePageResult handles only a single type.
52+
#if Handle_PageResultOfT
53+
config.GetHelpPageSampleGenerator().SampleObjectFactories.Add(GeneratePageResult);
54+
#endif
55+
56+
// Extend the following to use a preset object directly as the sample for all actions that support a media
57+
// type, regardless of the body parameter or return type. The lines below avoid display of binary content.
58+
// The BsonMediaTypeFormatter (if available) is not used to serialize the TextSample object.
59+
config.SetSampleForMediaType(
60+
new TextSample("Binary JSON content. See http://bsonspec.org for details."),
61+
new MediaTypeHeaderValue("application/bson"));
62+
63+
//// Uncomment the following to use "[0]=foo&[1]=bar" directly as the sample for all actions that support form URL encoded format
64+
//// and have IEnumerable<string> as the body parameter or return type.
65+
//config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));
66+
67+
//// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
68+
//// and action named "Put".
69+
//config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");
70+
71+
//// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
72+
//// on the controller named "Values" and action named "Get" with parameter "id".
73+
//config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");
74+
75+
//// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
76+
//// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
77+
//config.SetActualRequestType(typeof(string), "Values", "Get");
78+
79+
//// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
80+
//// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
81+
//config.SetActualResponseType(typeof(string), "Values", "Post");
82+
}
83+
84+
#if Handle_PageResultOfT
85+
private static object GeneratePageResult(HelpPageSampleGenerator sampleGenerator, Type type)
86+
{
87+
if (type.IsGenericType)
88+
{
89+
Type openGenericType = type.GetGenericTypeDefinition();
90+
if (openGenericType == typeof(PageResult<>))
91+
{
92+
// Get the T in PageResult<T>
93+
Type[] typeParameters = type.GetGenericArguments();
94+
Debug.Assert(typeParameters.Length == 1);
95+
96+
// Create an enumeration to pass as the first parameter to the PageResult<T> constuctor
97+
Type itemsType = typeof(List<>).MakeGenericType(typeParameters);
98+
object items = sampleGenerator.GetSampleObject(itemsType);
99+
100+
// Fill in the other information needed to invoke the PageResult<T> constuctor
101+
Type[] parameterTypes = new Type[] { itemsType, typeof(Uri), typeof(long?), };
102+
object[] parameters = new object[] { items, null, (long)ObjectGenerator.DefaultCollectionSize, };
103+
104+
// Call PageResult(IEnumerable<T> items, Uri nextPageLink, long? count) constructor
105+
ConstructorInfo constructor = type.GetConstructor(parameterTypes);
106+
return constructor.Invoke(parameters);
107+
}
108+
}
109+
110+
return null;
111+
}
112+
#endif
113+
}
114+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Web.Http;
3+
using System.Web.Mvc;
4+
using RandomDataWebApi.Areas.HelpPage.ModelDescriptions;
5+
using RandomDataWebApi.Areas.HelpPage.Models;
6+
7+
namespace RandomDataWebApi.Areas.HelpPage.Controllers
8+
{
9+
/// <summary>
10+
/// The controller that will handle requests for the help page.
11+
/// </summary>
12+
public class HelpController : Controller
13+
{
14+
private const string ErrorViewName = "Error";
15+
16+
public HelpController()
17+
: this(GlobalConfiguration.Configuration)
18+
{
19+
}
20+
21+
public HelpController(HttpConfiguration config)
22+
{
23+
Configuration = config;
24+
}
25+
26+
public HttpConfiguration Configuration { get; private set; }
27+
28+
public ActionResult Index()
29+
{
30+
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
31+
return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
32+
}
33+
34+
public ActionResult Api(string apiId)
35+
{
36+
if (!String.IsNullOrEmpty(apiId))
37+
{
38+
HelpPageApiModel apiModel = Configuration.GetHelpPageApiModel(apiId);
39+
if (apiModel != null)
40+
{
41+
return View(apiModel);
42+
}
43+
}
44+
45+
return View(ErrorViewName);
46+
}
47+
48+
public ActionResult ResourceModel(string modelName)
49+
{
50+
if (!String.IsNullOrEmpty(modelName))
51+
{
52+
ModelDescriptionGenerator modelDescriptionGenerator = Configuration.GetModelDescriptionGenerator();
53+
ModelDescription modelDescription;
54+
if (modelDescriptionGenerator.GeneratedModels.TryGetValue(modelName, out modelDescription))
55+
{
56+
return View(modelDescription);
57+
}
58+
}
59+
60+
return View(ErrorViewName);
61+
}
62+
63+
public ActionResult JsonTest()
64+
{
65+
return View();
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)