Angular 20 .NET 9 server-side rendering support

Discussão em 'Angular' iniciado por Dživo Jelić, Setembro 4, 2025.

  1. Anybody has any idea how to setup SSR Angular 20 with .NET 9 or even .NET 10 which is still in preview.

    I have implement SSR as discussed here https://angular.dev/guide/ssr.

    I have build output so browser and server folders in the Angular dist folder. What I don't get is how to now serve that using .NET.

    Do I serve browser/index.html and ssr magic works?

    Or do I have to mimic what has been written in server.ts file?

    import {
    AngularNodeAppEngine,
    createNodeRequestHandler,
    isMainModule,
    writeResponseToNodeResponse,
    } from '@angular/ssr/node';
    import express from 'express';
    import { join } from 'node:path';

    const browserDistFolder = join(import.meta.dirname, '../browser');

    const app = express();
    const angularApp = new AngularNodeAppEngine();

    /**
    * Example Express Rest API endpoints can be defined here.
    * Uncomment and define endpoints as necessary.
    *
    * Example:
    * ```ts
    * app.get('/api/{*splat}', (req, res) => {
    * // Handle API request
    * });
    * ```
    */

    /**
    * Serve static files from /browser
    */
    app.use(
    express.static(browserDistFolder, {
    maxAge: '1y',
    index: false,
    redirect: false,
    }),
    );

    /**
    * Handle all other requests by rendering the Angular application.
    */
    app.use((req, res, next) => {
    angularApp
    .handle(req)
    .then((response) =>
    response ? writeResponseToNodeResponse(response, res) : next(),
    )
    .catch(next);
    });

    /**
    * Start the server if this module is the main entry point.
    * The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
    */
    if (isMainModule(import.meta.url)) {
    const port = process.env['PORT'] || 4000;
    app.listen(port, (error) => {
    if (error) {
    throw error;
    }

    console.log(`Node Express server listening on http://localhost:${port}`);
    });
    }

    /**
    * Request handler used by the Angular CLI (for dev-server and during build) or Firebase Cloud Functions.
    */
    export const reqHandler = createNodeRequestHandler(app);


    One problem I get is that Angular is trying to find chunks in wrong location; I imagine that is problem with program.cs:

    var builder = WebApplication.CreateBuilder(args);
    var services = builder.Services;
    var configuration = builder.Configuration;

    var connectionStrings = RegisterOptions<ConnectionStrings>(services, configuration);

    services.ConfigureHttpJsonOptions(options =>
    {
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
    });

    services.AddOpenApi();
    services.AddCors();

    var app = builder.Build();

    if (app.Environment.IsDevelopment())
    {
    app.MapOpenApi();
    }

    app.MapScalarApiReference();
    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseCors(config => config
    .AllowAnyHeader()
    .AllowAnyMethod()
    .AllowAnyOrigin());

    app.MapFallbackToFile("index.html");

    app.Run();

    Continue reading...

Compartilhe esta Página