Remove the index.html from url asp .net react IIS

Jackie Moen 0 Reputation points
2023-09-22T19:33:55.8066667+00:00

I have an asp.net core and react project and I publish it to the IIS server. But after deploying it, it showed 'page can't be found' error. And when I added /index.html to the url (www.mywebsite.com/index.html), it showed my website and it seemed to be functioning.

Also, there's no wwwroot folder in my project and the wwwroot folder will be created on the IIS server when I publish the project.

Obviously, I don't want the user to enter /index.html when they access my website. I followed this tutorial but it didn't work https://dotnettutorials.net/lesson/configuring-default-page-asp-net-core/ .

Can anyone help me with that? Thanks!

Here's my program.cs file:

using Microsoft.EntityFrameworkCore;
using PslProcEng.Data;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;


var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();
builder.Services.AddControllersWithViews();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddDbContext<Context>(o => o.UseSqlServer(builder.Configuration.GetConnectionString("Connection")));
builder.Services.AddControllers().AddNewtonsoftJson();
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  app.UseHsts();
}

app.UseHttpsRedirection();

app.UseDefaultFiles();

app.UseStaticFiles(new StaticFileOptions
{
  FileProvider = new PhysicalFileProvider(Path.Combine(
AppDomain.CurrentDomain.BaseDirectory)),
});


app.UseRouting();

app.MapControllers();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html");

app.Run();
Internet Information Services
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,504 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 63,916 Reputation points
    2023-09-22T21:42:26.59+00:00

    the

    app.MapFallbackToFile("index.html");

    is the option to load the index.html if no route found. the is the asp.net core default. you can test this by publishing to a folder and running the app directly without IIS. if http://localhost:<sslport> doesn't work I'd suspect the static file handler config.

    if the app works standalone, then it appears IIS is not passing the request to asp.net core. You can test this if:

    https://www.mywebsite.com/

    works. then its IIS configuration. you will probably need to add index.html to IIS defaults.

    0 comments No comments

  2. Sam Wu-MSFT 7,446 Reputation points Microsoft Vendor
    2023-09-26T03:42:52.1+00:00

    @Jackie Moen

    You can use url rewrite to remove index.html in iis:

    <rule name="remove index.html " stopProcessing="true">      
       <match url="^(.*)index.html"/>                  
       <action type="Redirect" url="{R:1}"/>  
    </rule>
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.