Skip to content

Commit 84ccc4e

Browse files
committed
Revert "Revert to initial project template"
This reverts commit 02ef891.
1 parent 02ef891 commit 84ccc4e

File tree

11 files changed

+607
-2
lines changed

11 files changed

+607
-2
lines changed

RandomGenerator/RandomWebApp/App_Start/FilterConfig.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Web;
1+
using System;
2+
using System.Configuration;
3+
using System.Text.RegularExpressions;
4+
using System.Web;
25
using System.Web.Mvc;
36

47
namespace RandomWebApp
@@ -9,5 +12,35 @@ public static void RegisterGlobalFilters(GlobalFilterCollection filters)
912
{
1013
filters.Add(new HandleErrorAttribute());
1114
}
15+
16+
public static void ValidateUrlScheme(HttpApplication app)
17+
{
18+
if (!app.Request.IsSecureConnection &&
19+
RequireHttps &&
20+
!string.Equals(app.Request.Url.Host, "localhost", StringComparison.InvariantCultureIgnoreCase))
21+
{
22+
// Uri.OriginalString プロパティを使用すると、:80 が追加されてしまうことがあります。
23+
var secureUrl = Regex.Replace(app.Request.Url.AbsoluteUri, @"^\w+(?=://)", Uri.UriSchemeHttps);
24+
25+
if (PermanentHttps)
26+
{
27+
app.Response.RedirectPermanent(secureUrl, true);
28+
}
29+
else
30+
{
31+
app.Response.Redirect(secureUrl, true);
32+
}
33+
}
34+
}
35+
36+
static bool RequireHttps
37+
{
38+
get { return Convert.ToBoolean(ConfigurationManager.AppSettings["app:RequireHttps"]); }
39+
}
40+
41+
static bool PermanentHttps
42+
{
43+
get { return Convert.ToBoolean(ConfigurationManager.AppSettings["app:PermanentHttps"]); }
44+
}
1245
}
1346
}

RandomGenerator/RandomWebApp/App_Start/RouteConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public static void RegisterRoutes(RouteCollection routes)
1313
{
1414
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
1515

16+
routes.MapMvcAttributeRoutes();
17+
1618
routes.MapRoute(
1719
name: "Default",
1820
url: "{controller}/{action}/{id}",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.Mvc;
6+
7+
namespace RandomWebApp.Controllers
8+
{
9+
public class HomeController : Controller
10+
{
11+
public ActionResult Index()
12+
{
13+
return View();
14+
}
15+
16+
public ActionResult JsonTest()
17+
{
18+
return View();
19+
}
20+
21+
public ActionResult JsonpTest()
22+
{
23+
return View();
24+
}
25+
}
26+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using RandomLib;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Web;
6+
using System.Web.Mvc;
7+
8+
namespace RandomWebApp.Controllers
9+
{
10+
// Attribute Routing in ASP.NET MVC 5
11+
// http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
12+
[RoutePrefix("json")]
13+
[Route("{action=Index}")]
14+
public class RandomController : JsonApiController
15+
{
16+
public ActionResult Index()
17+
{
18+
return RedirectToAction("", "");
19+
}
20+
21+
// GET: /json/NewAlphabets/8
22+
[Route("NewAlphabets/{length:int:range(0,4096)}")]
23+
public ActionResult NewAlphabets(int length)
24+
{
25+
return Json(RandomUtility.GenerateAlphabets(length));
26+
}
27+
28+
// GET: /json/NewAlphanumerics/8
29+
[Route("NewAlphanumerics/{length:int:range(0,4096)}")]
30+
public ActionResult NewAlphanumerics(int length)
31+
{
32+
return Json(RandomUtility.GenerateAlphanumerics(length));
33+
}
34+
35+
// GET: /json/NewBytes/hexlower/16
36+
[Route("NewBytes/hexlower/{length:int:range(0,4096)}")]
37+
public ActionResult NewBytes_HexLower(int length)
38+
{
39+
var data = RandomUtility.GenerateBytes(length);
40+
return Json(data.ToHexString(false));
41+
}
42+
43+
// GET: /json/NewBytes/hexupper/16
44+
[Route("NewBytes/hexupper/{length:int:range(0,4096)}")]
45+
public ActionResult NewBytes_HexUpper(int length)
46+
{
47+
var data = RandomUtility.GenerateBytes(length);
48+
return Json(data.ToHexString(true));
49+
}
50+
51+
// GET: /json/NewBytes/base64/16
52+
[Route("NewBytes/base64/{length:int:range(0,4096)}")]
53+
public ActionResult NewBytes_Base64(int length)
54+
{
55+
var data = RandomUtility.GenerateBytes(length);
56+
return Json(Convert.ToBase64String(data));
57+
}
58+
59+
// GET: /json/NewUuid
60+
public ActionResult NewUuid()
61+
{
62+
return Json(Guid.NewGuid());
63+
}
64+
65+
// GET: /json/NewOrderedId
66+
public ActionResult NewOrderedId()
67+
{
68+
var id = RandomUtility.GenerateOrderedGuid();
69+
return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() });
70+
}
71+
72+
// GET: /json/NewOrderedId/sqlserver
73+
[Route("NewOrderedId/sqlserver")]
74+
public ActionResult NewOrderedId_SqlServer()
75+
{
76+
var id = RandomUtility.GenerateOrderedSqlGuid();
77+
return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() });
78+
}
79+
80+
// GET: /json/NewOrderedId2
81+
public ActionResult NewOrderedId2()
82+
{
83+
var id = RandomUtility.GenerateOrderedGuid2();
84+
return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() });
85+
}
86+
87+
// GET: /json/NewOrderedId2/sqlserver
88+
[Route("NewOrderedId2/sqlserver")]
89+
public ActionResult NewOrderedId2_SqlServer()
90+
{
91+
var id = RandomUtility.GenerateOrderedSqlGuid2();
92+
return Json(new { id = id.Guid, date = id.DateTime.ToIso8601String() });
93+
}
94+
}
95+
}

RandomGenerator/RandomWebApp/Global.asax.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,10 @@ protected void Application_Start()
2020
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
2121
RouteConfig.RegisterRoutes(RouteTable.Routes);
2222
}
23+
24+
void Application_BeginRequest(object sender, EventArgs e)
25+
{
26+
FilterConfig.ValidateUrlScheme(this);
27+
}
2328
}
2429
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.Mvc;
6+
7+
namespace RandomWebApp
8+
{
9+
public static class HtmlEx
10+
{
11+
public static MvcHtmlString TextLink(this HtmlHelper htmlHelper, string url)
12+
{
13+
var builder = new TagBuilder("a")
14+
{
15+
InnerHtml = HttpUtility.HtmlEncode(url),
16+
};
17+
builder.MergeAttribute("href", url);
18+
19+
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
20+
}
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.Mvc;
6+
using System.Web.Script.Serialization;
7+
8+
namespace RandomWebApp
9+
{
10+
public class JsonApiController : Controller
11+
{
12+
protected new ActionResult Json(object data)
13+
{
14+
Response.Headers["Expires"] = "-1";
15+
Response.Headers["Access-Control-Allow-Origin"] = "*";
16+
17+
var callback = Request.QueryString["callback"];
18+
19+
if (string.IsNullOrWhiteSpace(callback))
20+
{
21+
return Json(data, JsonRequestBehavior.AllowGet);
22+
}
23+
else
24+
{
25+
var serializer = new JavaScriptSerializer();
26+
var json = serializer.Serialize(data);
27+
return JavaScript(string.Format("{0}({1});", callback, json));
28+
}
29+
}
30+
}
31+
}

RandomGenerator/RandomWebApp/RandomWebApp.csproj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@
116116
</Reference>
117117
</ItemGroup>
118118
<ItemGroup>
119+
<Compile Include="Controllers\HomeController.cs" />
120+
<Compile Include="Controllers\RandomController.cs" />
119121
<Compile Include="Global.asax.cs">
120122
<DependentUpon>Global.asax</DependentUpon>
121123
</Compile>
124+
<Compile Include="HtmlEx.cs" />
125+
<Compile Include="JsonApiController.cs" />
122126
<Compile Include="Properties\AssemblyInfo.cs" />
123127
</ItemGroup>
124128
<ItemGroup>
@@ -137,12 +141,26 @@
137141
</ItemGroup>
138142
<ItemGroup>
139143
<Folder Include="App_Data\" />
140-
<Folder Include="Controllers\" />
141144
<Folder Include="Models\" />
142145
</ItemGroup>
143146
<ItemGroup>
144147
<Content Include="packages.config" />
145148
</ItemGroup>
149+
<ItemGroup>
150+
<Content Include="Views\Home\Index.cshtml" />
151+
</ItemGroup>
152+
<ItemGroup>
153+
<ProjectReference Include="..\RandomLib\RandomLib.csproj">
154+
<Project>{c5ba44d2-4dd0-47d2-9d08-ce2c2783a95f}</Project>
155+
<Name>RandomLib</Name>
156+
</ProjectReference>
157+
</ItemGroup>
158+
<ItemGroup>
159+
<Content Include="Views\Home\JsonTest.cshtml" />
160+
</ItemGroup>
161+
<ItemGroup>
162+
<Content Include="Views\Home\JsonpTest.cshtml" />
163+
</ItemGroup>
146164
<PropertyGroup>
147165
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
148166
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

0 commit comments

Comments
 (0)