From f20806571ebbe37906d1e8a5cd622927df51481d Mon Sep 17 00:00:00 2001 From: Keiho Sakapon Date: Thu, 4 Jan 2018 21:17:22 +0900 Subject: [PATCH 1/4] Move source code --- RandomGenerator/RandomGenerator.sln | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/RandomGenerator/RandomGenerator.sln b/RandomGenerator/RandomGenerator.sln index def1a28..05d5c20 100644 --- a/RandomGenerator/RandomGenerator.sln +++ b/RandomGenerator/RandomGenerator.sln @@ -15,21 +15,6 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomConsole", "RandomConsole\RandomConsole.csproj", "{5C9CE8D8-F9F0-4F5C-9B0B-85AB4B8E0EB6}" EndProject Global - GlobalSection(TeamFoundationVersionControl) = preSolution - SccNumberOfProjects = 4 - SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C} - SccTeamFoundationServer = https://saka-pon-apps14.visualstudio.com/defaultcollection - SccLocalPath0 = . - SccProjectUniqueName1 = RandomWebApp\\RandomWebApp.csproj - SccProjectName1 = RandomWebApp - SccLocalPath1 = RandomWebApp - SccProjectUniqueName2 = RandomLib\\RandomLib.csproj - SccProjectName2 = RandomLib - SccLocalPath2 = RandomLib - SccProjectUniqueName3 = RandomConsole\\RandomConsole.csproj - SccProjectName3 = RandomConsole - SccLocalPath3 = RandomConsole - EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU From 574987d7bd17795787f26de09c44107d7d19fcda Mon Sep 17 00:00:00 2001 From: Keiho Sakapon Date: Thu, 4 Jan 2018 21:21:49 +0900 Subject: [PATCH 2/4] Move source code --- RandomGenerator/RandomWebApp/RandomWebApp.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/RandomGenerator/RandomWebApp/RandomWebApp.csproj b/RandomGenerator/RandomWebApp/RandomWebApp.csproj index d749936..dce39f5 100644 --- a/RandomGenerator/RandomWebApp/RandomWebApp.csproj +++ b/RandomGenerator/RandomWebApp/RandomWebApp.csproj @@ -20,10 +20,6 @@ - SAK - SAK - SAK - SAK ..\ true From 02ef8913cda45832978ac7c633f9e2b718d9d71f Mon Sep 17 00:00:00 2001 From: Keiho Sakapon Date: Thu, 4 Jan 2018 21:44:12 +0900 Subject: [PATCH 3/4] Revert to initial project template --- .../RandomWebApp/App_Start/FilterConfig.cs | 35 +--- .../RandomWebApp/App_Start/RouteConfig.cs | 2 - .../Controllers/HomeController.cs | 26 --- .../Controllers/RandomController.cs | 95 ----------- RandomGenerator/RandomWebApp/Global.asax.cs | 5 - RandomGenerator/RandomWebApp/HtmlEx.cs | 22 --- .../RandomWebApp/JsonApiController.cs | 31 ---- .../RandomWebApp/RandomWebApp.csproj | 20 +-- .../RandomWebApp/Views/Home/Index.cshtml | 153 ----------------- .../RandomWebApp/Views/Home/JsonTest.cshtml | 155 ------------------ .../RandomWebApp/Views/Home/JsonpTest.cshtml | 65 -------- 11 files changed, 2 insertions(+), 607 deletions(-) delete mode 100644 RandomGenerator/RandomWebApp/Controllers/HomeController.cs delete mode 100644 RandomGenerator/RandomWebApp/Controllers/RandomController.cs delete mode 100644 RandomGenerator/RandomWebApp/HtmlEx.cs delete mode 100644 RandomGenerator/RandomWebApp/JsonApiController.cs delete mode 100644 RandomGenerator/RandomWebApp/Views/Home/Index.cshtml delete mode 100644 RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml delete mode 100644 RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml diff --git a/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs b/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs index 1e94a09..b13bc04 100644 --- a/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs +++ b/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs @@ -1,7 +1,4 @@ -using System; -using System.Configuration; -using System.Text.RegularExpressions; -using System.Web; +using System.Web; using System.Web.Mvc; namespace RandomWebApp @@ -12,35 +9,5 @@ public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } - - public static void ValidateUrlScheme(HttpApplication app) - { - if (!app.Request.IsSecureConnection && - RequireHttps && - !string.Equals(app.Request.Url.Host, "localhost", StringComparison.InvariantCultureIgnoreCase)) - { - // Uri.OriginalString プロパティを使用すると、:80 が追加されてしまうことがあります。 - var secureUrl = Regex.Replace(app.Request.Url.AbsoluteUri, @"^\w+(?=://)", Uri.UriSchemeHttps); - - if (PermanentHttps) - { - app.Response.RedirectPermanent(secureUrl, true); - } - else - { - app.Response.Redirect(secureUrl, true); - } - } - } - - static bool RequireHttps - { - get { return Convert.ToBoolean(ConfigurationManager.AppSettings["app:RequireHttps"]); } - } - - static bool PermanentHttps - { - get { return Convert.ToBoolean(ConfigurationManager.AppSettings["app:PermanentHttps"]); } - } } } \ No newline at end of file diff --git a/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs b/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs index 5fe0202..5c19dd0 100644 --- a/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs +++ b/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs @@ -13,8 +13,6 @@ public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); - routes.MapMvcAttributeRoutes(); - routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", diff --git a/RandomGenerator/RandomWebApp/Controllers/HomeController.cs b/RandomGenerator/RandomWebApp/Controllers/HomeController.cs deleted file mode 100644 index 5de7f5f..0000000 --- a/RandomGenerator/RandomWebApp/Controllers/HomeController.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; - -namespace RandomWebApp.Controllers -{ - public class HomeController : Controller - { - public ActionResult Index() - { - return View(); - } - - public ActionResult JsonTest() - { - return View(); - } - - public ActionResult JsonpTest() - { - return View(); - } - } -} diff --git a/RandomGenerator/RandomWebApp/Controllers/RandomController.cs b/RandomGenerator/RandomWebApp/Controllers/RandomController.cs deleted file mode 100644 index 3726363..0000000 --- a/RandomGenerator/RandomWebApp/Controllers/RandomController.cs +++ /dev/null @@ -1,95 +0,0 @@ -using RandomLib; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; - -namespace RandomWebApp.Controllers -{ - // Attribute Routing in ASP.NET MVC 5 - // http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx - [RoutePrefix("json")] - [Route("{action=Index}")] - public class RandomController : JsonApiController - { - public ActionResult Index() - { - return RedirectToAction("", ""); - } - - // GET: /json/NewAlphabets/8 - [Route("NewAlphabets/{length:int:range(0,4096)}")] - public ActionResult NewAlphabets(int length) - { - return Json(RandomUtility.GenerateAlphabets(length)); - } - - // GET: /json/NewAlphanumerics/8 - [Route("NewAlphanumerics/{length:int:range(0,4096)}")] - public ActionResult NewAlphanumerics(int length) - { - return Json(RandomUtility.GenerateAlphanumerics(length)); - } - - // GET: /json/NewBytes/hexlower/16 - [Route("NewBytes/hexlower/{length:int:range(0,4096)}")] - public ActionResult NewBytes_HexLower(int length) - { - var data = RandomUtility.GenerateBytes(length); - return Json(data.ToHexString(false)); - } - - // GET: /json/NewBytes/hexupper/16 - [Route("NewBytes/hexupper/{length:int:range(0,4096)}")] - public ActionResult NewBytes_HexUpper(int length) - { - var data = RandomUtility.GenerateBytes(length); - return Json(data.ToHexString(true)); - } - - // GET: /json/NewBytes/base64/16 - [Route("NewBytes/base64/{length:int:range(0,4096)}")] - public ActionResult NewBytes_Base64(int length) - { - var data = RandomUtility.GenerateBytes(length); - return Json(Convert.ToBase64String(data)); - } - - // GET: /json/NewUuid - public ActionResult NewUuid() - { - return Json(Guid.NewGuid()); - } - - // GET: /json/NewOrderedId - public ActionResult NewOrderedId() - { - var id = RandomUtility.GenerateOrderedGuid(); - return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); - } - - // GET: /json/NewOrderedId/sqlserver - [Route("NewOrderedId/sqlserver")] - public ActionResult NewOrderedId_SqlServer() - { - var id = RandomUtility.GenerateOrderedSqlGuid(); - return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); - } - - // GET: /json/NewOrderedId2 - public ActionResult NewOrderedId2() - { - var id = RandomUtility.GenerateOrderedGuid2(); - return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); - } - - // GET: /json/NewOrderedId2/sqlserver - [Route("NewOrderedId2/sqlserver")] - public ActionResult NewOrderedId2_SqlServer() - { - var id = RandomUtility.GenerateOrderedSqlGuid2(); - return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); - } - } -} diff --git a/RandomGenerator/RandomWebApp/Global.asax.cs b/RandomGenerator/RandomWebApp/Global.asax.cs index 33089e1..5395e84 100644 --- a/RandomGenerator/RandomWebApp/Global.asax.cs +++ b/RandomGenerator/RandomWebApp/Global.asax.cs @@ -20,10 +20,5 @@ protected void Application_Start() FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); } - - void Application_BeginRequest(object sender, EventArgs e) - { - FilterConfig.ValidateUrlScheme(this); - } } } \ No newline at end of file diff --git a/RandomGenerator/RandomWebApp/HtmlEx.cs b/RandomGenerator/RandomWebApp/HtmlEx.cs deleted file mode 100644 index 26a429a..0000000 --- a/RandomGenerator/RandomWebApp/HtmlEx.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; - -namespace RandomWebApp -{ - public static class HtmlEx - { - public static MvcHtmlString TextLink(this HtmlHelper htmlHelper, string url) - { - var builder = new TagBuilder("a") - { - InnerHtml = HttpUtility.HtmlEncode(url), - }; - builder.MergeAttribute("href", url); - - return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal)); - } - } -} diff --git a/RandomGenerator/RandomWebApp/JsonApiController.cs b/RandomGenerator/RandomWebApp/JsonApiController.cs deleted file mode 100644 index 2deffb2..0000000 --- a/RandomGenerator/RandomWebApp/JsonApiController.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; -using System.Web.Script.Serialization; - -namespace RandomWebApp -{ - public class JsonApiController : Controller - { - protected new ActionResult Json(object data) - { - Response.Headers["Expires"] = "-1"; - Response.Headers["Access-Control-Allow-Origin"] = "*"; - - var callback = Request.QueryString["callback"]; - - if (string.IsNullOrWhiteSpace(callback)) - { - return Json(data, JsonRequestBehavior.AllowGet); - } - else - { - var serializer = new JavaScriptSerializer(); - var json = serializer.Serialize(data); - return JavaScript(string.Format("{0}({1});", callback, json)); - } - } - } -} diff --git a/RandomGenerator/RandomWebApp/RandomWebApp.csproj b/RandomGenerator/RandomWebApp/RandomWebApp.csproj index dce39f5..579bc6b 100644 --- a/RandomGenerator/RandomWebApp/RandomWebApp.csproj +++ b/RandomGenerator/RandomWebApp/RandomWebApp.csproj @@ -116,13 +116,9 @@ - - Global.asax - - @@ -141,26 +137,12 @@ + - - - - - - {c5ba44d2-4dd0-47d2-9d08-ce2c2783a95f} - RandomLib - - - - - - - - 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/RandomGenerator/RandomWebApp/Views/Home/Index.cshtml b/RandomGenerator/RandomWebApp/Views/Home/Index.cshtml deleted file mode 100644 index e8a5e94..0000000 --- a/RandomGenerator/RandomWebApp/Views/Home/Index.cshtml +++ /dev/null @@ -1,153 +0,0 @@ -@using RandomWebApp -@{ - Layout = null; - ViewBag.Title = "Random Data Web API"; -} - - - - - - - @ViewBag.Title - - -
-

@ViewBag.Title

-

Provides the JSON/JSONP API to generate random data.

-

Interfaces

-
-
NewAlphabets
-
-

Creates a new alphabets with the specified length.

-
    -
  • - @Html.TextLink("/json/NewAlphabets/8") -

    - e.g. -

    @("\"jVNXbgEA\"")
    -

    -
  • -
-
-
NewAlphanumerics
-
-

Creates a new alphanumerics with the specified length.

-
    -
  • - @Html.TextLink("/json/NewAlphanumerics/8") -

    - e.g. -

    @("\"RzZbOj7s\"")
    -

    -
  • -
-
-
NewBytes
-
-

Creates a new byte sequence with the specified length.

-
    -
  • - @Html.TextLink("/json/NewBytes/hexlower/16") -

    - The lowercase hexadecimal format.
    - e.g. -

    @("\"9a605681933a5d3c66cb65dd052331ef\"")
    -

    -
  • -
  • - @Html.TextLink("/json/NewBytes/hexupper/16") -

    - The uppercase hexadecimal format.
    - e.g. -

    @("\"9A605681933A5D3C66CB65DD052331EF\"")
    -

    -
  • -
  • - @Html.TextLink("/json/NewBytes/base64/16") -

    - The Base64 format.
    - e.g. -

    @("\"I9K7dQKQkUWx+aCavrFilQ==\"")
    -

    -
  • -
-
-
NewUuid
-
-

Creates a new UUID (GUID).

-
    -
  • - @Html.TextLink("/json/NewUuid") -

    - A version 4 UUID.
    - e.g. -

    @("\"b018469a-98a7-4d51-9283-bf9efdea27be\"")
    -

    -
  • -
-
-
NewOrderedId
-
-

- Creates a new time-ordered 16-byte ID with the UUID format.
- 8 bytes of them represent ticks of date/time (by 10-7 second) and the other 8 bytes are randomly generated.
- Returns a pair of the ID and the created date/time. -

-
    -
  • - @Html.TextLink("/json/NewOrderedId") -

    - The upper 8 bytes represent ticks of date/time.
    - e.g. -

    @("{\"id\":\"08d11c74-45cf-762f-e0f5-d5010081a7b1\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    -

    -
  • -
  • - @Html.TextLink("/json/NewOrderedId/sqlserver") -

    - Orders values in the uniqueidentifier data type in SQL Server.
    - The lower 8 bytes represent ticks of date/time.
    - e.g. -

    @("{\"id\":\"e0f5d501-0081-a7b1-762f-08d11c7445cf\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    -

    -
  • -
-
-
NewOrderedId2
-
-

- Creates a new time-ordered 16-byte ID with the UUID format.
- 6 bytes of them represent ticks of date/time (by about 10-3 second) and the other 10 bytes are randomly generated.
- Returns a pair of the ID and the created date/time. -

-
    -
  • - @Html.TextLink("/json/NewOrderedId2") -

    - The upper 6 bytes represent ticks of date/time.
    - e.g. -

    @("{\"id\":\"8d11c744-5cf7-29b5-e0f5-d5010081a7b1\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    -

    -
  • -
  • - @Html.TextLink("/json/NewOrderedId2/sqlserver") -

    - Orders values in the uniqueidentifier data type in SQL Server.
    - The lower 6 bytes represent ticks of date/time.
    - e.g. -

    @("{\"id\":\"e0f5d501-0081-a7b1-29b5-8d11c7445cf7\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    -

    -
  • -
-
-
-

Test

-

@Html.ActionLink("Test Page", "JsonTest")

-

@Html.ActionLink("Test Page for JSONP", "JsonpTest")

-
-
-

© 2014 Keiho Sakapon.

-
- - diff --git a/RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml b/RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml deleted file mode 100644 index 7720440..0000000 --- a/RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml +++ /dev/null @@ -1,155 +0,0 @@ -@{ - Layout = null; - ViewBag.Title = "Random Data Web API - Test"; -} - - - - - - - @ViewBag.Title - - - -
-

@ViewBag.Title

-

This Web API supports CORS.

-

Domain: @Html.TextBox("TargetDomain", string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority))

-
-
NewAlphabets
-
-

- String length: @Html.TextBox("NewAlphabetsLength", 8) - -

-
 
-
-
NewAlphanumerics
-
-

- String length: @Html.TextBox("NewAlphanumericsLength", 8) - -

-
 
-
-
NewBytes (lowercase hexadecimal)
-
-

- Byte length: @Html.TextBox("NewBytesHexLowerLength", 16) - -

-
 
-
-
NewBytes (uppercase hexadecimal)
-
-

- Byte length: @Html.TextBox("NewBytesHexUpperLength", 16) - -

-
 
-
-
NewBytes (Base64)
-
-

- Byte length: @Html.TextBox("NewBytesBase64Length", 16) - -

-
 
-
-
NewUuid
-
-

- -

-
 
-
-
NewOrderedId
-
-

- -

-
 
-
-
NewOrderedId (SQL Server)
-
-

- -

-
 
-
-
NewOrderedId2
-
-

- -

-
 
-
-
NewOrderedId2 (SQL Server)
-
-

- -

-
 
-
-
-
-
-

© 2014 Keiho Sakapon.

-
- - - diff --git a/RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml b/RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml deleted file mode 100644 index 11ff5e4..0000000 --- a/RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml +++ /dev/null @@ -1,65 +0,0 @@ -@{ - Layout = null; - ViewBag.Title = "Random Data Web API - JSONP Test"; -} - - - - - - - @ViewBag.Title - - - -
-

@ViewBag.Title

-

Domain: @Html.TextBox("TargetDomain", string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority))

-
-
NewUuid
-
-

- -

-
 
-
-
NewOrderedId
-
-

- -

-
 
-
-
NewOrderedId2
-
-

- -

-
 
-
-
-
-
-

© 2014 Keiho Sakapon.

-
- - - From 84ccc4e8a68d8b06388139d4a21d4388cc5039fd Mon Sep 17 00:00:00 2001 From: Keiho Sakapon Date: Thu, 4 Jan 2018 21:48:42 +0900 Subject: [PATCH 4/4] Revert "Revert to initial project template" This reverts commit 02ef8913cda45832978ac7c633f9e2b718d9d71f. --- .../RandomWebApp/App_Start/FilterConfig.cs | 35 +++- .../RandomWebApp/App_Start/RouteConfig.cs | 2 + .../Controllers/HomeController.cs | 26 +++ .../Controllers/RandomController.cs | 95 +++++++++++ RandomGenerator/RandomWebApp/Global.asax.cs | 5 + RandomGenerator/RandomWebApp/HtmlEx.cs | 22 +++ .../RandomWebApp/JsonApiController.cs | 31 ++++ .../RandomWebApp/RandomWebApp.csproj | 20 ++- .../RandomWebApp/Views/Home/Index.cshtml | 153 +++++++++++++++++ .../RandomWebApp/Views/Home/JsonTest.cshtml | 155 ++++++++++++++++++ .../RandomWebApp/Views/Home/JsonpTest.cshtml | 65 ++++++++ 11 files changed, 607 insertions(+), 2 deletions(-) create mode 100644 RandomGenerator/RandomWebApp/Controllers/HomeController.cs create mode 100644 RandomGenerator/RandomWebApp/Controllers/RandomController.cs create mode 100644 RandomGenerator/RandomWebApp/HtmlEx.cs create mode 100644 RandomGenerator/RandomWebApp/JsonApiController.cs create mode 100644 RandomGenerator/RandomWebApp/Views/Home/Index.cshtml create mode 100644 RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml create mode 100644 RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml diff --git a/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs b/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs index b13bc04..1e94a09 100644 --- a/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs +++ b/RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs @@ -1,4 +1,7 @@ -using System.Web; +using System; +using System.Configuration; +using System.Text.RegularExpressions; +using System.Web; using System.Web.Mvc; namespace RandomWebApp @@ -9,5 +12,35 @@ public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } + + public static void ValidateUrlScheme(HttpApplication app) + { + if (!app.Request.IsSecureConnection && + RequireHttps && + !string.Equals(app.Request.Url.Host, "localhost", StringComparison.InvariantCultureIgnoreCase)) + { + // Uri.OriginalString プロパティを使用すると、:80 が追加されてしまうことがあります。 + var secureUrl = Regex.Replace(app.Request.Url.AbsoluteUri, @"^\w+(?=://)", Uri.UriSchemeHttps); + + if (PermanentHttps) + { + app.Response.RedirectPermanent(secureUrl, true); + } + else + { + app.Response.Redirect(secureUrl, true); + } + } + } + + static bool RequireHttps + { + get { return Convert.ToBoolean(ConfigurationManager.AppSettings["app:RequireHttps"]); } + } + + static bool PermanentHttps + { + get { return Convert.ToBoolean(ConfigurationManager.AppSettings["app:PermanentHttps"]); } + } } } \ No newline at end of file diff --git a/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs b/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs index 5c19dd0..5fe0202 100644 --- a/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs +++ b/RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs @@ -13,6 +13,8 @@ public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); + routes.MapMvcAttributeRoutes(); + routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", diff --git a/RandomGenerator/RandomWebApp/Controllers/HomeController.cs b/RandomGenerator/RandomWebApp/Controllers/HomeController.cs new file mode 100644 index 0000000..5de7f5f --- /dev/null +++ b/RandomGenerator/RandomWebApp/Controllers/HomeController.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +namespace RandomWebApp.Controllers +{ + public class HomeController : Controller + { + public ActionResult Index() + { + return View(); + } + + public ActionResult JsonTest() + { + return View(); + } + + public ActionResult JsonpTest() + { + return View(); + } + } +} diff --git a/RandomGenerator/RandomWebApp/Controllers/RandomController.cs b/RandomGenerator/RandomWebApp/Controllers/RandomController.cs new file mode 100644 index 0000000..3726363 --- /dev/null +++ b/RandomGenerator/RandomWebApp/Controllers/RandomController.cs @@ -0,0 +1,95 @@ +using RandomLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +namespace RandomWebApp.Controllers +{ + // Attribute Routing in ASP.NET MVC 5 + // http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx + [RoutePrefix("json")] + [Route("{action=Index}")] + public class RandomController : JsonApiController + { + public ActionResult Index() + { + return RedirectToAction("", ""); + } + + // GET: /json/NewAlphabets/8 + [Route("NewAlphabets/{length:int:range(0,4096)}")] + public ActionResult NewAlphabets(int length) + { + return Json(RandomUtility.GenerateAlphabets(length)); + } + + // GET: /json/NewAlphanumerics/8 + [Route("NewAlphanumerics/{length:int:range(0,4096)}")] + public ActionResult NewAlphanumerics(int length) + { + return Json(RandomUtility.GenerateAlphanumerics(length)); + } + + // GET: /json/NewBytes/hexlower/16 + [Route("NewBytes/hexlower/{length:int:range(0,4096)}")] + public ActionResult NewBytes_HexLower(int length) + { + var data = RandomUtility.GenerateBytes(length); + return Json(data.ToHexString(false)); + } + + // GET: /json/NewBytes/hexupper/16 + [Route("NewBytes/hexupper/{length:int:range(0,4096)}")] + public ActionResult NewBytes_HexUpper(int length) + { + var data = RandomUtility.GenerateBytes(length); + return Json(data.ToHexString(true)); + } + + // GET: /json/NewBytes/base64/16 + [Route("NewBytes/base64/{length:int:range(0,4096)}")] + public ActionResult NewBytes_Base64(int length) + { + var data = RandomUtility.GenerateBytes(length); + return Json(Convert.ToBase64String(data)); + } + + // GET: /json/NewUuid + public ActionResult NewUuid() + { + return Json(Guid.NewGuid()); + } + + // GET: /json/NewOrderedId + public ActionResult NewOrderedId() + { + var id = RandomUtility.GenerateOrderedGuid(); + return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); + } + + // GET: /json/NewOrderedId/sqlserver + [Route("NewOrderedId/sqlserver")] + public ActionResult NewOrderedId_SqlServer() + { + var id = RandomUtility.GenerateOrderedSqlGuid(); + return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); + } + + // GET: /json/NewOrderedId2 + public ActionResult NewOrderedId2() + { + var id = RandomUtility.GenerateOrderedGuid2(); + return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); + } + + // GET: /json/NewOrderedId2/sqlserver + [Route("NewOrderedId2/sqlserver")] + public ActionResult NewOrderedId2_SqlServer() + { + var id = RandomUtility.GenerateOrderedSqlGuid2(); + return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() }); + } + } +} diff --git a/RandomGenerator/RandomWebApp/Global.asax.cs b/RandomGenerator/RandomWebApp/Global.asax.cs index 5395e84..33089e1 100644 --- a/RandomGenerator/RandomWebApp/Global.asax.cs +++ b/RandomGenerator/RandomWebApp/Global.asax.cs @@ -20,5 +20,10 @@ protected void Application_Start() FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); } + + void Application_BeginRequest(object sender, EventArgs e) + { + FilterConfig.ValidateUrlScheme(this); + } } } \ No newline at end of file diff --git a/RandomGenerator/RandomWebApp/HtmlEx.cs b/RandomGenerator/RandomWebApp/HtmlEx.cs new file mode 100644 index 0000000..26a429a --- /dev/null +++ b/RandomGenerator/RandomWebApp/HtmlEx.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +namespace RandomWebApp +{ + public static class HtmlEx + { + public static MvcHtmlString TextLink(this HtmlHelper htmlHelper, string url) + { + var builder = new TagBuilder("a") + { + InnerHtml = HttpUtility.HtmlEncode(url), + }; + builder.MergeAttribute("href", url); + + return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal)); + } + } +} diff --git a/RandomGenerator/RandomWebApp/JsonApiController.cs b/RandomGenerator/RandomWebApp/JsonApiController.cs new file mode 100644 index 0000000..2deffb2 --- /dev/null +++ b/RandomGenerator/RandomWebApp/JsonApiController.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.Web.Script.Serialization; + +namespace RandomWebApp +{ + public class JsonApiController : Controller + { + protected new ActionResult Json(object data) + { + Response.Headers["Expires"] = "-1"; + Response.Headers["Access-Control-Allow-Origin"] = "*"; + + var callback = Request.QueryString["callback"]; + + if (string.IsNullOrWhiteSpace(callback)) + { + return Json(data, JsonRequestBehavior.AllowGet); + } + else + { + var serializer = new JavaScriptSerializer(); + var json = serializer.Serialize(data); + return JavaScript(string.Format("{0}({1});", callback, json)); + } + } + } +} diff --git a/RandomGenerator/RandomWebApp/RandomWebApp.csproj b/RandomGenerator/RandomWebApp/RandomWebApp.csproj index 579bc6b..dce39f5 100644 --- a/RandomGenerator/RandomWebApp/RandomWebApp.csproj +++ b/RandomGenerator/RandomWebApp/RandomWebApp.csproj @@ -116,9 +116,13 @@ + + Global.asax + + @@ -137,12 +141,26 @@ - + + + + + + {c5ba44d2-4dd0-47d2-9d08-ce2c2783a95f} + RandomLib + + + + + + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/RandomGenerator/RandomWebApp/Views/Home/Index.cshtml b/RandomGenerator/RandomWebApp/Views/Home/Index.cshtml new file mode 100644 index 0000000..e8a5e94 --- /dev/null +++ b/RandomGenerator/RandomWebApp/Views/Home/Index.cshtml @@ -0,0 +1,153 @@ +@using RandomWebApp +@{ + Layout = null; + ViewBag.Title = "Random Data Web API"; +} + + + + + + + @ViewBag.Title + + +
+

@ViewBag.Title

+

Provides the JSON/JSONP API to generate random data.

+

Interfaces

+
+
NewAlphabets
+
+

Creates a new alphabets with the specified length.

+
    +
  • + @Html.TextLink("/json/NewAlphabets/8") +

    + e.g. +

    @("\"jVNXbgEA\"")
    +

    +
  • +
+
+
NewAlphanumerics
+
+

Creates a new alphanumerics with the specified length.

+
    +
  • + @Html.TextLink("/json/NewAlphanumerics/8") +

    + e.g. +

    @("\"RzZbOj7s\"")
    +

    +
  • +
+
+
NewBytes
+
+

Creates a new byte sequence with the specified length.

+
    +
  • + @Html.TextLink("/json/NewBytes/hexlower/16") +

    + The lowercase hexadecimal format.
    + e.g. +

    @("\"9a605681933a5d3c66cb65dd052331ef\"")
    +

    +
  • +
  • + @Html.TextLink("/json/NewBytes/hexupper/16") +

    + The uppercase hexadecimal format.
    + e.g. +

    @("\"9A605681933A5D3C66CB65DD052331EF\"")
    +

    +
  • +
  • + @Html.TextLink("/json/NewBytes/base64/16") +

    + The Base64 format.
    + e.g. +

    @("\"I9K7dQKQkUWx+aCavrFilQ==\"")
    +

    +
  • +
+
+
NewUuid
+
+

Creates a new UUID (GUID).

+
    +
  • + @Html.TextLink("/json/NewUuid") +

    + A version 4 UUID.
    + e.g. +

    @("\"b018469a-98a7-4d51-9283-bf9efdea27be\"")
    +

    +
  • +
+
+
NewOrderedId
+
+

+ Creates a new time-ordered 16-byte ID with the UUID format.
+ 8 bytes of them represent ticks of date/time (by 10-7 second) and the other 8 bytes are randomly generated.
+ Returns a pair of the ID and the created date/time. +

+
    +
  • + @Html.TextLink("/json/NewOrderedId") +

    + The upper 8 bytes represent ticks of date/time.
    + e.g. +

    @("{\"id\":\"08d11c74-45cf-762f-e0f5-d5010081a7b1\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    +

    +
  • +
  • + @Html.TextLink("/json/NewOrderedId/sqlserver") +

    + Orders values in the uniqueidentifier data type in SQL Server.
    + The lower 8 bytes represent ticks of date/time.
    + e.g. +

    @("{\"id\":\"e0f5d501-0081-a7b1-762f-08d11c7445cf\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    +

    +
  • +
+
+
NewOrderedId2
+
+

+ Creates a new time-ordered 16-byte ID with the UUID format.
+ 6 bytes of them represent ticks of date/time (by about 10-3 second) and the other 10 bytes are randomly generated.
+ Returns a pair of the ID and the created date/time. +

+
    +
  • + @Html.TextLink("/json/NewOrderedId2") +

    + The upper 6 bytes represent ticks of date/time.
    + e.g. +

    @("{\"id\":\"8d11c744-5cf7-29b5-e0f5-d5010081a7b1\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    +

    +
  • +
  • + @Html.TextLink("/json/NewOrderedId2/sqlserver") +

    + Orders values in the uniqueidentifier data type in SQL Server.
    + The lower 6 bytes represent ticks of date/time.
    + e.g. +

    @("{\"id\":\"e0f5d501-0081-a7b1-29b5-8d11c7445cf7\",\"date\":\"2014-04-02T10:20:14.8958767Z\"}")
    +

    +
  • +
+
+
+

Test

+

@Html.ActionLink("Test Page", "JsonTest")

+

@Html.ActionLink("Test Page for JSONP", "JsonpTest")

+
+
+

© 2014 Keiho Sakapon.

+
+ + diff --git a/RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml b/RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml new file mode 100644 index 0000000..7720440 --- /dev/null +++ b/RandomGenerator/RandomWebApp/Views/Home/JsonTest.cshtml @@ -0,0 +1,155 @@ +@{ + Layout = null; + ViewBag.Title = "Random Data Web API - Test"; +} + + + + + + + @ViewBag.Title + + + +
+

@ViewBag.Title

+

This Web API supports CORS.

+

Domain: @Html.TextBox("TargetDomain", string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority))

+
+
NewAlphabets
+
+

+ String length: @Html.TextBox("NewAlphabetsLength", 8) + +

+
 
+
+
NewAlphanumerics
+
+

+ String length: @Html.TextBox("NewAlphanumericsLength", 8) + +

+
 
+
+
NewBytes (lowercase hexadecimal)
+
+

+ Byte length: @Html.TextBox("NewBytesHexLowerLength", 16) + +

+
 
+
+
NewBytes (uppercase hexadecimal)
+
+

+ Byte length: @Html.TextBox("NewBytesHexUpperLength", 16) + +

+
 
+
+
NewBytes (Base64)
+
+

+ Byte length: @Html.TextBox("NewBytesBase64Length", 16) + +

+
 
+
+
NewUuid
+
+

+ +

+
 
+
+
NewOrderedId
+
+

+ +

+
 
+
+
NewOrderedId (SQL Server)
+
+

+ +

+
 
+
+
NewOrderedId2
+
+

+ +

+
 
+
+
NewOrderedId2 (SQL Server)
+
+

+ +

+
 
+
+
+
+
+

© 2014 Keiho Sakapon.

+
+ + + diff --git a/RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml b/RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml new file mode 100644 index 0000000..11ff5e4 --- /dev/null +++ b/RandomGenerator/RandomWebApp/Views/Home/JsonpTest.cshtml @@ -0,0 +1,65 @@ +@{ + Layout = null; + ViewBag.Title = "Random Data Web API - JSONP Test"; +} + + + + + + + @ViewBag.Title + + + +
+

@ViewBag.Title

+

Domain: @Html.TextBox("TargetDomain", string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority))

+
+
NewUuid
+
+

+ +

+
 
+
+
NewOrderedId
+
+

+ +

+
 
+
+
NewOrderedId2
+
+

+ +

+
 
+
+
+
+
+

© 2014 Keiho Sakapon.

+
+ + +