Reputation: 3483
I created an ASP.Net Core 2.2 Web Api Project and it is running locally without any issue. After I publish it to the file system, it is always giving me 404 issue. I have enabled windows fetures related to IIS and asp.net framework web api2 applications are running well in the same server.
I have enabled swagger doc and has used Microsoft.AspNetCore.Authentication libs too.
Program.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace US.BOX.AuthAPI
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Startup.cs
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using US.BOX.AuthAPI.Extensions;
namespace US.BOX.AuthAPI
{
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IConfiguration 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)
{
services.Configure<IISOptions>(options =>
{
options.ForwardClientCertificate = false;
});
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IAuthenticationSchemeProvider, CustomAuthenticationSchemeProvider>();
services.AddSwaggerDocumentation();
services.AddJwtBearerAuthentication(_configuration);
services.AddCors();
services.AddLogging();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ILogger<Startup> logger)
{
app.UseAuthentication();
if (env.IsDevelopment())
{
app.UseSwaggerDocumentation();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
appsettings.json
{
"JWT": {
// TODO: This should be updated for production deployment
"SecurityKey": "sDIkdjhkalUthsaCVjsdfiskokrge",
"Issuer": "https://{host_name}:{port}",
"Audience": "https://{host_name}:{port}",
"ExpirationTimeInMinutes": 60
},
"Logging": {
"LogFilePath": "Logs/auth-{Date}.txt",
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
UsersController.cs
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace US.BOX.AuthAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
[HttpGet]
public IActionResult GetAll()
{
try
{
return Ok("Users");
}
catch (Exception)
{
throw;
}
}
}
}
After I published it generates following web.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\US.BOX.AuthAPI.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
</configuration>
Upvotes: 3
Views: 5221
Reputation: 3483
Hope this will help someone. Followings are the steps I did for solving this issue.
Enable IIS Hostable Web Core
Install ‘Visual C++ Redistributable for Visual Studio 2015’
https://www.microsoft.com/en-us/download/details.aspx?id=48145
Install ‘dotnet-hosting-2.2.6-win.exe’
Create separate IIS Application pool with ‘No Manage Code’ in .NET CLR Version
** Note order of doing above steps is very important
Upvotes: 2
Reputation: 36
below are some of the checklist you can check if you have done it.
see if the above solves the issue. revert for any queries vote and like if your issue is resolved so that it might help someone.
Upvotes: 2