I have issues running my asp.net core 3.1 web api on a remote server after publishing it. The API is working well on my PC but when I publish it, all API end points are returning error 404. My swagger documentation page swagger/index.html is also returning the same error.
Below is the Program.cs code
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
My Startup cs file is as follows
public class Startup
{
private readonly IWebHostEnvironment _env;
private readonly IConfiguration _configuration;
//public static IMvcBuilder ConfigureApiBehaviorOptions(this IMvcBuilder builder, Action<ApiBehaviorOptions> setupAction);
public Startup(IWebHostEnvironment env, IConfiguration configuration)
{
_env = env;
_configuration = configuration;
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
try
{
// use sql server db in production and sqlite db in development
//if (_env.IsProduction())
// services.AddDbContext<DataContext, SqlDataContext>();
//else
// services.AddDbContext<DataContext, SqliteDataContext>();
//services.AddDbContext<DataContext, SqlDataContext>();
services.AddDbContext<DataContext, SqlDataContext>
(options => options.UseSqlServer(Configuration.GetConnectionString("SQLServerdb")));
services.AddCors();
services.AddControllers();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// configure strongly typed settings objects
var appSettingsSection = _configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
// enabling API documentation
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "v1",
Title = "x",
Description = "x",
TermsOfService = null,
Contact = new Microsoft.OpenApi.Models.OpenApiContact()
{
Name = "x",
Email = "x",
Url = null
}
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
// enabling Cookies
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
}).AddCookie("Cookies", options =>
{
options.Cookie.Name = globals.APISessionKey;
options.Cookie.SameSite = SameSiteMode.None;
options.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = redirectContext =>
{
redirectContext.HttpContext.Response.StatusCode = 401;
return Task.CompletedTask;
}
};
});
// configure jwt authentication
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.Events = new JwtBearerEvents()
{
OnTokenValidated = context =>
{
var userService = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
var theContext = context.HttpContext;
var userId = int.Parse(context.Principal.Identity.Name);
var user = userService.GetById(context.HttpContext, userId);
if (user == null)
{
// return unauthorized if user no longer exists
context.Fail("Unauthorized");
}
return Task.CompletedTask;
}
};
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
// custom validation responses CustomBadRequest
//services.Configure<ApiBehaviorOptions>(a =>
//{
// a.InvalidModelStateResponseFactory = context =>
// {
// var problemDetails = new CustomBadRequest(context);
// return new BadRequestObjectResult(problemDetails)
// {
// // ContentTypes = { "application / problem + json", "application / problem + xml" } // these requires cross origin resource what what (cors)
// };
// };
//});
}
catch (Exception ex)
{
Console.WriteLine("ConfigureServices _ERROR: " + ex.Message);
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext dataContext)
{
try
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
dataContext.Database.Migrate();
app.UseHttpsRedirection();
app.UseRouting();
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// Enable middle ware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1 (RELEASE)");
//c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1 (DEBUG)");
/*To serve the Swagger UI at the app's root (http://localhost:<port>/), set the RoutePrefix property to an empty string:
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;*/
});
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
//app.UseHttpsRedirection();
//app.UseRouting();
//app.UseAuthorization();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllers();
//});
}
catch (Exception ex)
{
Console.WriteLine("Configure _ERROR: " + ex.Message);
}
}
}
}
Is there anything that i am missing. My API is haring the same solution with a blazor server app which is working perfectly well on the same server which the API is failing to work.
I have tried fixing the issue on my own and searching on the internet but haven't found anything that could assist me resolve this issue. Your help is highly appreciated.