1

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 the following error:

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

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

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

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
    {
        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've created folders logs\stdout, however, logs file were not created.

I am little bit stuck. Could you say what am I doing wrong?

Learner
  • 417
  • 6
  • 24
  • Update it to 'app.UseDeveloperExceptionPage();' even when in prod or check logs to get some more detailed info on whats goes wrong. You no longer need a web.config file. Try removing that and see what effect it has. – CountZero May 21 '20 at 08:29
  • @CountZero thank you for your comment! However, I already have `app.UseDeveloperExceptionPage();` at the bottom of `Configure()` method. Do you want to move it after `app.UseHsts();` code? – Learner May 21 '20 at 08:46
  • Maybe move it to the top, often ordering is important when configuring middleware. Try removing the web config altogether - not usually required on .net core apps. Can you check logs on your hosting provider to see if there are more detailed exception detauils. – CountZero May 21 '20 at 09:03
  • @CountZero **Maybe move it to the top** - ok, I did it, however the error is still the same. **Try removing the web config altogether** ok, I did it and error now says `403 - Forbidden: Access is denied. You do not have permission to view this directory or page using the credentials that you supplied.` **Can you check logs on your hosting provider to see if there are more detailed exception detauils**- there are no errors in `logs\stdout` folder. – Learner May 21 '20 at 09:26
  • There are some good instruction to read here. https://www.smarterasp.net/support/kb/a1775/asp_net-core-hosting.aspx You don't need a web.config unless you want IIS acting as a proxy - I'd avoid it unless you need the featured. – CountZero May 21 '20 at 09:35
  • Remove the 'app.UseAuthentication();' and the last error should go away. – CountZero May 21 '20 at 09:37
  • @CountZero **Remove the 'app.UseAuthentication();' ** thanks for advice, however the error is still the same `403 - Forbidden: Access is denied. You do not have permission to view this directory or page using the credentials that you supplied.` – Learner May 21 '20 at 10:01
  • 1
    Could be lots of things. When I end up in this situation I reduce the app down to the most simple one possible then add functionality until I find the thing causing the issues. Try deploying a hello world app and see if that works. Then add your features until it breaks. Can you deploy and run this example? https://learn.microsoft.com/en-us/visualstudio/ide/quickstart-aspnet-core?view=vs-2019 – CountZero May 21 '20 at 10:24
  • @CountZero thank you man, I will try to do it. I am just wondering how errors could be shown as it will help to know the origin or the error – Learner May 21 '20 at 10:45
  • 1
    We've made some progress - you know you're getting a 403 - the question is - is it your application sending that out or some setting on your hosting provider. – CountZero May 21 '20 at 11:27
  • 1
    Remove both of these from ConfigureServices too if present AddAuthentication AddAuthorization – CountZero May 21 '20 at 11:28
  • 1
    Also check in the control panel of your hosting provider and see if there are any auth settings in there that might be causing it. – CountZero May 21 '20 at 11:29
  • 1
    @CountZero thank you for your kindness. You are very cool! Thanks! As there is no answer, I just upvoted your answers. **Remove both of these from ConfigureServices too if present AddAuthentication AddAuthorization** I will try to do it – Learner May 21 '20 at 12:10
  • No worries mate, happy to help :) – CountZero May 21 '20 at 12:28
  • @CountZero ooops, now I have `HTTP Error 403.14 - Forbidden`. Maybe you have some thoughts? Thanks! – Learner May 21 '20 at 13:01

3 Answers3

3

I got similar type of problem and i fixed this by providing complete path of dotnet exe in config file. update process path according to your server path:

<aspNetCore processPath="C:\Program Files\dotnet\dotnet.exe" arguments=".\fooappl.dll" stdoutLogEnabled="true" 
      stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />

Try once!

Always_a_learner
  • 1,254
  • 1
  • 8
  • 16
3

I've managed to deploy my application! Maybe it will help to others.

So my mistakes were:

  1. Not correct web.config. Thanks to guys from SmarterAsp.Net! This is the correct web.config which shows 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-->
    
  2. After web.config was fixed, then detailed errors are shown. And error was FileNotFoundException. The reason was that Directory.GetCurrentDirectory() works incorrectly.

So I've changed 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"))
    });
}
  1. I've added folder wwwroot inside of my ASP.NET Core 2.2 application and put Angular prod bundle into it

If somebody would need a guide how to deploy from Visual Studio

Learner
  • 417
  • 6
  • 24
1

Try removing hostingModel="InProcess" from your web.config file.