Skip to content

Instantly share code, notes, and snippets.

@VaclavElias
Last active January 7, 2023 15:36
Show Gist options
  • Save VaclavElias/544b8d097981e217ae0dc7c32adba705 to your computer and use it in GitHub Desktop.
Save VaclavElias/544b8d097981e217ae0dc7c32adba705 to your computer and use it in GitHub Desktop.
ASP.NET Core 2.1 + Angular 6 in MVC Razor Pages

ASP.NET Core 2.1 + Angular 6 in MVC Razor Pages

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

  1. Install ASP.NET Core 2.1 + Angular Template
  2. Test application
  3. Update to Angular 6 http://www.talkingdotnet.com/upgrade-angular-5-app-angular-6-visual-studio-2017/
  4. Test application
  5. 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",
  1. Merge above to new ASP.NET Core 2.1 RC1 + MVC + Authentication Template
  2. 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
  1. 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>
  1. One of my actions in DashboardController
[Authorize]
public IActionResult Index() => View();
  1. 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);
}
  1. 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.

@mhkoca
Copy link

mhkoca commented Nov 28, 2020

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", "/"))

@VaclavElias
Copy link
Author

Thanks @mhkoca, fixed.

@rmjoia
Copy link

rmjoia commented Jul 22, 2021

Sorry I got late to the party, but is this still working for other .net (core) versions? Do you have a repo with this solution? I had to point to the build resources to get it working, the angular app never starts even in development

@VaclavElias
Copy link
Author

Hi @rmjoia, I cannot share my repo but I have got this running on .NET 5 + Angular 11. I think with even less ceremony. I could prepare an example repo in a few days as I am a bit busy right now..

@rmjoia
Copy link

rmjoia commented Jul 22, 2021

I'll keep looking for a solution, cheers :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment