ASP.NET Core 2.2 Web API Angular. Hosting provider says 500 - Internal Server Error
I'm trying to publish an ASP.NET Core 2.2 Web API and Angular 8 project using SmarterASP.NET, a hosting provider that supports ASP.NET Core. However, I get the following error:
500 Internal Server Error. There is a problem with the resource you are looking for and cannot be displayed.
However, if I start the project locally it works perfectly.
I searched through the internet and various forums and saw this question , another question and this github post .
Here is my Main
method:
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
WebHost.CreateDefaultBuilder(args)
.UseSetting(WebHostDefaults.DetailedErrorsKey, "true");
host.Run();
}
}
Here is my Configure()
class StartUp
:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
IServiceProvider provider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 &&
!Path.HasExtension(context.Request.Path.Value))
{
context.Request.Path = "/index.html";
await next();
}
});
app.ConfigureCustomExceptionMiddleware();
app.UseCors("ApiCorsPolicy");
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles")),
RequestPath = new PathString("/StaticFiles")
});
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
app.UseDeveloperExceptionPage();
}
Here is my web.config
file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<httpErrors errorMode="Detailed" />
<aspNetCore processPath="dotnet">
<environmentVariables>
<environmentVariable name="ASPNETCORE_DETAILEDERRORS" value="true" />
</environmentVariables>
</aspNetCore>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2"
resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\fooappl.dll" stdoutLogEnabled="true"
stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
<!--ProjectGuid: 25e0e882-c3ff-4f8d-98cc-f26a692fa019-->
I have created the folder logs\stdout
but the log file is not created.
I'm kind of stuck. Can you say what am I doing wrong?
I have successfully deployed my application! Maybe it will help others.
So my error is:
incorrect
web.config
. Thanks to the guys at SmarterAsp.Net! This is correctweb.config
, it shows the detailed error:<?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=".\yourAppl.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" > <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> </environmentVariables> </aspNetCore> </system.webServer> </location> </configuration> <!--ProjectGuid: 25e0e882-c3ff-4f8d-98cc-f26a692fa019-->
After
web.config
that was fixed, bugs were shown and then detailed. The error isFileNotFoundException
. The reason is thatDirectory.GetCurrentDirectory()
it doesn't work correctly.
So I changed the code to:
private IHostingEnvironment _env;
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
_env = env;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider provider)
{
// The other code is omitted for the brevity
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(_env.ContentRootPath, @"StaticFiles")),
RequestPath = new PathString(Path.Combine(_env.ContentRootPath, "/StaticFiles"))
});
}
- I have added folder
wwwroot
in my ASP.NET Core 2.2 application and put Angular prod package in it
If anyone needs guidance on how to deploy from Visual Studio