Search code examples
asp.net-corehttp-status-code-404web-deploymentnetlifyblazor-webassembly

404 Deploying asp.net core hosted blazor webassembly to Netlify


I am attempting to deploy an asp.net core hosted blazor webassembly app to Netlify. I have published the release version of the Server project to a directory on my desktop, and uploaded it to github. I set Netlify's publish directory to wwwroot and the site does render just fine. However, if I attempt a call to an api controller, it returns a 404. Specifically, here is my code:

    //Register.razor in the Client project
    if (Model.Password.Length >= 6 && Model.Password == Model.ConfirmPassword)
    {
        await HttpClient.PostAsJsonAsync<RegisterModel>("api/Register/Post", Model);
        NavigationManager.NavigateTo("/");
    }

    //In my controller
    [Route("api/Register")]
    public class RegisterController : Controller
    {
        private UserContext UserContext { get; set; }
        private IHasher Hasher = new Pbkdf2Hasher();
        public RegisterController (UserContext userContext)
        {
            UserContext = userContext;
        }

        [RequireHttps]
        [HttpPost]
        [Route("Post")]
        public async Task Post([FromBody]RegisterModel model)
        {
            var user = new UserModel
            {
                Email = model.Email,
                Password = Hasher.Hash(model.Password)
            };
            await UserContext.AddAsync(user);
            await UserContext.SaveChangesAsync();
        }
    }

The url request I send is: https://(NetlifyDefaultDomain)/api/Register/Post. However, I get a 404 response. On localhost it works just fine. I'm imagining that there's a setting that I have to change somewhere in order for the request URL to work. I've tried looking but have been unable to find guidance. What do I need to change? Thanks

Edit

Here is the Program.cs file of my Client project

public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("app");
            builder.Services.AddTransient(sp => new HttpClient { 
            BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
            builder.Services.AddBlazoredLocalStorage();
            builder.Services.AddAuthorizationCore();
            builder.Services.AddScoped<AuthenticationStateProvider, 
            ApiAuthenticationStateProvider>();
            builder.Services.AddScoped<IAuthService, AuthService>();
            await builder.Build().RunAsync();
        }
    }

Target framework is netstandard2.1, and it's webassembly 3.2.0.


Solution

  • Netlify is a static file host. They do not run any server-side applications like .NET core on their servers.

    So you can host your Blazor client-side application on Netlify but if you want server side code to run you must host it somewhere else.

    If you are looking for free hosting for your API there are some cloud providers that have a free tier. Azure has free App Service with some limits, Google cloud has a free micro VPS that can host a small app, heroku also has a free offering.

    A cheap VPS from Digital Ocean, Vultr or Linode is another alternative.