Vite configuration using minimal hosting model
d

Dan

over 1 year ago
Hi, I'm using Vite to handle the front-end assets on a new site, and using Vite.AspNetCore for the middleware. This set-up works on a v13 project which was upgraded from v12 using both Startup.cs and Program.cs files, but doesn't on a brand new v13 project using the minimal hosting model. The Program.cs on the new site is:
using Vite.AspNetCore;
using Vite.AspNetCore.Extensions;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddDeliveryApi()
    .AddComposers()
    .Build();

builder.Services.AddViteServices(new ViteOptions()
{
    Server = new ViteServerOptions
    {
        Port = 5173,
        Https = true,
    }
});

WebApplication app = builder.Build();

await app.BootUmbracoAsync();

app.UseHttpsRedirection();

app.UseUmbraco()
    .WithMiddleware(u =>
    {
        u.UseBackOffice();
        u.UseWebsite();
    })
    .WithEndpoints(u =>
    {
        u.UseInstallerEndpoints();
        u.UseBackOfficeEndpoints();
        u.UseWebsiteEndpoints();
    });

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

await app.RunAsync();
Everything else is pretty much the same (similar`package.json`, identical
vite.config.js
, same folder structure, same reserved paths [including
~/@vite/,~/@id/
] in
appsettings.Development.json
etc). The assets are output from Master.cshtml file like this:
<environment include="Development">
    <script type="module" defer src="~/@@vite/client"></script> <!-- Load Vite -->
    <script type="module" defer src="~/scripts/main.js"></script> <!-- Define the entry point -->
</environment>
However, when running the new site both
/@vite/client
and
/scripts/main.js
are throwing 404s. The middleware configuration (above) looks okay AFAIK and all else is very similar so I'm struggling to understand why one works and the other doesn't. Could anyone suggest what might be awry here?
[Solved] Api controller backoffice user is null when firing requests though swagger.
j

Jack

over 1 year ago
Hello this is my first post. I am trying v14 of Umbraco, and have been following the tutorial on how to add a custom swagger document. Article: https://docs.umbraco.com/umbraco-cms/tutorials/creating-a-backoffice-api/adding-a-custom-swagger-document I created my first simple api controller "logout" and so far so good it shows up on the swagger dashboard. I authorize myself and try requesting from swagger, at first i get a 401, which i don't understand, as i am logged in as an admin.
csharp
namespace Test.Web.Controllers.Api
{
    [ApiController]
    [ApiVersion("1.0")]
    [MapToApi("test-v1")]
    [Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
    [JsonOptionsName(Constants.JsonOptionsNames.BackOffice)]
    [Route("api/v{version:apiVersion}/test")]
    public class LogoutController : Controller
    {
        private readonly INotificationService _notificationService;
        private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public LogoutController(IBackOfficeSecurityAccessor backOfficeSecurityAccessor, INotificationService notificationService, IHttpContextAccessor httpContextAccessor)
        {
            _backOfficeSecurityAccessor = backOfficeSecurityAccessor;
            _notificationService = notificationService;
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpPost("logout")]
        [MapToApiVersion("1.0")]
        [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
        public IActionResult Logout()
        {
            IUser? user = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser;
            if (user == null)
            {
                return Unauthorized();
            }

            return Ok();
        }
    }
}
The policy [Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)] Does not seem to be related/assigned to my role, what am i missing ?