Skip to content

Commit 87e10a6

Browse files
committed
Add an API route so we can use both WebAPI and MVC with a single target page.
Remove shared layout page as we're only going to have one page - why have two files to maintain? Use main-bower-files to pull in bower dependencies rather than manually entering them into the gulp file.
1 parent 91a0a90 commit 87e10a6

File tree

7 files changed

+108
-70
lines changed

7 files changed

+108
-70
lines changed

src/AngularGettingStarted/Controllers/HomeController.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,39 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Microsoft.AspNet.Mvc;
6+
using Microsoft.AspNet.Hosting;
7+
using AngularGettingStarted.Models;
68

79
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
810

911
namespace AngularGettingStarted.Controllers
1012
{
1113
public class HomeController : Controller
1214
{
15+
IHostingEnvironment m_hostingEnv;
16+
dynamic m_envInfo;
17+
18+
public HomeController(IHostingEnvironment hostingEnv)
19+
{
20+
m_hostingEnv = hostingEnv;
21+
m_envInfo = new EnvInfo()
22+
{
23+
Development = m_hostingEnv.IsDevelopment(),
24+
Production = m_hostingEnv.IsProduction(),
25+
Version = "1.0.0-alpha0"
26+
};
27+
}
28+
1329
// GET: /<controller>/
1430
public IActionResult Index()
1531
{
16-
return View("Index");
32+
33+
return View("Index", m_envInfo);
34+
}
35+
36+
public dynamic ApiDetails()
37+
{
38+
return new { API = true };
1739
}
1840
}
1941
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"profiles": {
3+
"IIS Express": {
4+
"commandName": "IISExpress",
5+
"launchBrowser": true,
6+
"environmentVariables": {
7+
"ASPNET_ENV": "Development"
8+
}
9+
}
10+
}
11+
}

src/AngularGettingStarted/Startup.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,33 @@
66
using Microsoft.AspNet.Http;
77
using Microsoft.Framework.DependencyInjection;
88
using Microsoft.AspNet.Hosting;
9+
using Microsoft.AspNet.Routing.Constraints;
910

1011
namespace AngularGettingStarted
1112
{
1213
public class Startup
1314
{
14-
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
1515
public void ConfigureServices(IServiceCollection services)
1616
{
1717
services.AddMvc();
1818
}
1919

2020
public void Configure(IApplicationBuilder app)
2121
{
22+
// Serve our static assets with the highest priority - JS files, CSS files, etc.
2223
app.UseStaticFiles();
24+
25+
// Use MVC for the remaining routes.
2326
app.UseMvc(routes =>
2427
{
28+
// First, create a default API route for our WebAPI endpoints - this will handle anything
29+
// starting with api/.
30+
routes.MapRoute("api",
31+
template: "api/{controller}/{action}/{id?}",
32+
defaults: new { controller = "Home", action = "ApiDetails" });
33+
34+
// Now map *all* remaining routes to the default controller, as they'll all
35+
// route to our single-page application.
2536
routes.MapRoute("default",
2637
template: "{*url}",
2738
defaults: new { controller = "Home", action = "Index" });
Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1-
@*
2-
For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
3-
*@
4-
@{
5-
Layout = "~/Views/Shared/_Layout.cshtml";
6-
}
7-
@{
8-
// ViewBag.Title = "Home Page";
9-
}
10-
Hello world.
1+
@model AngularGettingStarted.Models.EnvInfo
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta name="viewport" content="width=device-width" />
6+
<title>@ViewBag.Title</title>
7+
@if (Model.Development)
8+
{
9+
10+
<!-- inject:css -->
11+
<link rel="stylesheet" href="/css/src/site.css">
12+
<!-- endinject -->
13+
<!-- inject:js -->
14+
<script src="/lib/angular/angular.js"></script>
15+
<script src="/lib/angular-ui-router/release/angular-ui-router.js"></script>
16+
<!-- endinject -->
17+
<!-- app:js -->
18+
<script src="/js/src/app.js"></script>
19+
<!-- endinject -->
20+
21+
}
22+
@if (!Model.Development)
23+
{
24+
25+
<!-- min:css -->
26+
<link rel="stylesheet" href="/css/app.min.css">
27+
<!-- endinject -->
28+
<!-- min:js -->
29+
<script src="/js/libs.min.js"></script>
30+
<script src="/js/app.min.js"></script>
31+
<!-- endinject -->
32+
33+
}
34+
</head>
35+
<body>
36+
<div>
37+
Hello World
38+
</div>
39+
</body>
40+
</html>

src/AngularGettingStarted/Views/Shared/_Layout.cshtml

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/AngularGettingStarted/gulpfile.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/// <binding BeforeBuild='inject' Clean='clean' />
2-
32
var gulp = require("gulp"),
43
rimraf = require("rimraf"),
54
concat = require("gulp-concat"),
65
cssmin = require("gulp-cssmin"),
76
uglify = require("gulp-uglify"),
87
inject = require("gulp-inject"),
98
angularFilesort = require("gulp-angular-filesort"),
9+
mainBowerFiles = require("main-bower-files"),
1010
project = require("./project.json");
1111

1212
var paths = {
@@ -18,14 +18,11 @@ paths.css = [paths.webroot + "css/src/**/*.css"];
1818
paths.concatJsDest = paths.webroot + "js/app.min.js";
1919
paths.concatCssDest = paths.webroot + "css/app.min.css";
2020

21+
// Inject the scripts into any view with the appropriate comments in them.
2122
paths.htmlInject = ["./Views/**/*.cshtml"];
2223

23-
// When adding Bower dependencies, ensure that whatever JS and CSS files
24-
// they provide are added to this list.
25-
paths.libJs = [
26-
paths.webroot + 'lib/angular/angular.min.js',
27-
paths.webroot + 'lib/angular-ui-router/release/angular-ui-router.min.js'
28-
];
24+
// When adding Bower dependencies, if they have any CSS files, need to add them to
25+
// this array to get included.
2926
paths.libCss = [];
3027

3128
paths.concatLibJsDest = paths.webroot + "js/libs.min.js";
@@ -34,62 +31,65 @@ paths.concatLibCssDest = paths.webroot + "css/libs.min.css";
3431
paths.concatAll = [paths.concatLibCssDest, paths.concatCssDest, paths.concatLibJsDest, paths.concatJsDest];
3532

3633
gulp.task("clean:js", function (cb) {
37-
rimraf(paths.concatJsDest, cb);
34+
return rimraf(paths.concatJsDest, cb);
3835
});
3936

4037
gulp.task("clean:css", function (cb) {
41-
rimraf(paths.concatCssDest, cb);
38+
return rimraf(paths.concatCssDest, cb);
4239
});
4340

4441
gulp.task("clean:libJs", function (cb) {
45-
rimraf(paths.concatLibJsDest, cb);
42+
return rimraf(paths.concatLibJsDest, cb);
4643
});
4744

4845
gulp.task("clean:libCss", function (cb) {
49-
rimraf(paths.concatLibCssDest, cb);
46+
return rimraf(paths.concatLibCssDest, cb);
5047
});
5148

5249
gulp.task("clean", ["clean:js", "clean:css", "clean:libJs", "clean:libCss"]);
5350

5451
gulp.task("min:js", function () {
55-
gulp.src(paths.js, { base: "." })
52+
return gulp.src(paths.js, { base: "." })
5653
.pipe(angularFilesort())
5754
.pipe(concat(paths.concatJsDest))
5855
.pipe(uglify())
5956
.pipe(gulp.dest("."));
6057
});
6158

6259
gulp.task("min:css", function () {
63-
gulp.src(paths.css, { base: "." })
60+
return gulp.src(paths.css, { base: "." })
6461
.pipe(concat(paths.concatCssDest))
6562
.pipe(cssmin())
6663
.pipe(gulp.dest("."));
6764
});
6865

6966
gulp.task("min:libJs", function () {
70-
gulp.src(paths.libJs, { base: "." })
67+
return gulp.src(mainBowerFiles())
7168
.pipe(concat(paths.concatLibJsDest))
69+
.pipe(uglify())
7270
.pipe(gulp.dest("."));
7371
});
7472

7573
gulp.task("min:libCss", function () {
76-
gulp.src(paths.libCss, { base: "." })
74+
return gulp.src(paths.libCss, { base: "." })
7775
.pipe(concat(paths.concatLibCssDest))
7876
.pipe(cssmin())
7977
.pipe(gulp.dest("."));
8078
});
8179

8280
gulp.task("min", ["min:js", "min:css", "min:libJs", "min:libCss"]);
8381

84-
//gulp.task("inject", ["inject:libJsAndCss", "inject:appJs"]);
85-
8682
gulp.task("inject", ["min"], function () {
87-
// JS libs then CSS libs then our CSS
88-
var sources = gulp.src(paths.libJs.concat(paths.libCss).concat(paths.css), { base: ".", read: false });
83+
// Dev: JS libs then CSS libs then our CSS.
84+
var sources = gulp.src(mainBowerFiles().concat(paths.libCss).concat(paths.css), { base: ".", read: false });
85+
// Dev: Our JS files.
8986
var appSources = gulp.src(paths.js, { base: "." }).pipe(angularFilesort());
87+
// Prod: All minified JS (libs and ours), and CSS.
9088
var minSources = gulp.src(paths.concatAll, { base: ".", read: false });
91-
gulp.src(paths.htmlInject, { base: "." })
92-
.pipe(inject(sources, { ignorePath: project.webroot }))
89+
90+
// Substitute into the relevant view files.
91+
return gulp.src(paths.htmlInject, { base: "." })
92+
.pipe(inject(sources, { ignorePath: project.webroot })) // Direct
9393
.pipe(inject(appSources, { name: "app", ignorePath: project.webroot }))
9494
.pipe(inject(minSources, { name: "min", ignorePath: project.webroot }))
9595
.pipe(gulp.dest("."));

src/AngularGettingStarted/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"gulp-uglify": "~1.4.1",
1010
"gulp-inject": "~3.0.0",
1111
"gulp-angular-filesort": "~1.1.1",
12+
"main-bower-files": "~2.9.0",
1213
"rimraf": "~2.4.3"
1314
}
1415
}

0 commit comments

Comments
 (0)