locked
ASP.NET Core 2.2 Web API Angular. Hosting provider says 500 - Internal server error. RRS feed

  • Question

  • User1725160972 posted

    Summary of the problem I am having:

    I am trying to publish ASP.NET Core 2.2 Web API and Angular 8 project with a hosting provider SmarterASP.NET that supports ASP.NET Core. However, I am getting an error.

    However, the project works perfectly if I start it locally.

    I searched through the Internet and various forums and see this questionanother one question and this github post.

    Error I am receiving:

    500 - Internal server error.
    There is a problem with the resource you are looking for, and it cannot be displayed.

    My code:

    This 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();
        }
    }

    This is my `Configure()` of `StartUp` class:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
        IServiceProvider provider)
    {
        if (env.IsDevelopment())
        {
    	app.UseDeveloperExceptionPage();
        }
        else
        {
    	// 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.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();
    }

    And this 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 am little bit stuck. Could you say what am I doing wrong?

    Thursday, May 21, 2020 8:41 AM

Answers

  • User753101303 posted

    Hi,

    You have UseDeveloperExceptionPage twice? ConfigureCustomExceptionMiddleware is not supposed to log exceptions somewhere? 

    http 500 usually means you have a server side exception and the first step should be to look at where exceptions are logged (for example for non shared Windows server, the Application event log). You can now look at the source code knowing what to look for rather than trying to guess which is usually much longer.

    If I'm not finding an exception message where it is supposed to be I then look at the IIS log to see if it could be some other 500.x error and look for example at https://support.microsoft.com/en-us/help/943891/the-http-status-code-in-iis-7-0-iis-7-5-and-iis-8-0

    In short always start first from the ACTUAL error that happens rather than reading first the code and trying to guess which error(s) could happen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 12:28 PM

All replies

  • User753101303 posted

    Hi,

    You have UseDeveloperExceptionPage twice? ConfigureCustomExceptionMiddleware is not supposed to log exceptions somewhere? 

    http 500 usually means you have a server side exception and the first step should be to look at where exceptions are logged (for example for non shared Windows server, the Application event log). You can now look at the source code knowing what to look for rather than trying to guess which is usually much longer.

    If I'm not finding an exception message where it is supposed to be I then look at the IIS log to see if it could be some other 500.x error and look for example at https://support.microsoft.com/en-us/help/943891/the-http-status-code-in-iis-7-0-iis-7-5-and-iis-8-0

    In short always start first from the ACTUAL error that happens rather than reading first the code and trying to guess which error(s) could happen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 12:28 PM
  • User1120430333 posted

    https://stackify.com/csharp-catch-all-exceptions/

    You can catch the unhandled exception and log it. You can use something like Serilog to log the exception, the stack trace and the inner,execption.message if available.

    Thursday, May 21, 2020 2:33 PM