Other optional ways:
http://www.talkingdotnet.com/add-angular-6-material-angular-6-asp-net-core-app/ http://www.talkingdotnet.com/upgrade-angular-5-app-angular-6-visual-studio-2017/ http://www.mithunvp.com/using-angular-elements-asp-net-core-angular-cli-visual-studio/ https://neelbhatt.com/2018/06/02/create-an-application-with-angular-6-and-net-core-step-by-step-guide/ https://www.codeproject.com/Articles/1250961/Deploying-an-Angular-Application-with-ASP-NET-Core
- Install ASP.NET Core 2.1 + Angular Template
- Test application
- Update to Angular 6 http://www.talkingdotnet.com/upgrade-angular-5-app-angular-6-visual-studio-2017/
- Test application
- Make following updates, --extract-css is not working and we want to remove hashing with --output-hashing-none
from
"start": "ng serve --extract-css",
"build": "ng build --extract-css",
to
"start": "ng serve",
"build": "ng build --output-hashing=none",
- Merge above to new ASP.NET Core 2.1 RC1 + MVC + Authentication Template
- Create Views/Shared/_AngularPartial.cshtml and add these
<script asp-append-version="true" src="~/runtime.js"></script>
<script asp-append-version="true" src="~/polyfills.js"></script>
<script asp-append-version="true" src="~/styles.js"></script>
<script asp-append-version="true" src="~/vendor.js"></script>
<script asp-append-version="true" src="~/main.js"></script>
// asp-append-version is not working here
- Create Views/Dashboard/Index.cshtml and add these
@{
Layout = null;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Application Name</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<div class="row"><div class="col-sm-12"><h1>Hello From Dashboard 1 :)</h1></div></div>
<div class="row">
<div class="col-sm-12">
<app-root>Loading Dashboard 1...</app-root>
</div>
</div>
<partial name="_AngularPartial" />
</body>
</html>
- One of my actions in DashboardController
[Authorize]
public IActionResult Index() => View();
- Startup.cs
from
app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}});
to
app.MapWhen(context => IsSpa(context.Request.Path.Value) && context.User.Identity.IsAuthenticated, builder =>
{
builder.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
});
bool IsSpa(string pathValue)
{
var endsWith = new[] { ".woff2", ".woff", "ttf" };
var angularFiles = new[] { "/main.js", "/vendor.js", "/polyfills.js", "/runtime.js", "/styles.js", "/styles.css" };
var angularPages = new[] { "/dashboard", "/dashboard2" };
return angularPages.Contains(pathValue) || angularFiles.Contains(pathValue)
|| pathValue.Contains("sockjs-node")
|| endsWith.Any(pathValue.EndsWith);
}
- I have also added this as it is published automatically and I don't want to access angular index.html from outside of MVC.
..
app.UseRewriter(new RewriteOptions().AddRedirect("index.html", "/"))
..
I have to publish it and test.
there is a little typo on end of story:
app.UseRewriter(new RewriteOptions().AddRedirct("index.html", "/"))
should be
app.UseRewriter(new RewriteOptions().AddRedirect("index.html", "/"))