0

I am new to ASP.Net Core and trying to host my first simple MVC app in IIS. I have installed the IIS hosting bundle; published my MVC project to a folder using Visual Studio and then, created an IIS website with it's physical path set to the published folder. When I test it using url http://localhost:5008/home/CallApi, however, I am getting a 500 error that says "This page isn’t working. localhost is currently unable to handle this request.". I can run the MVC app from within VS (selfhost, not through IIS, on a different port), it works fine there. What am I doing wrong here?

Alexu
  • 1,015
  • 2
  • 12
  • 32
  • Very likely you got the wrong URL. You might also run a report to reveal common issues, https://docs.jexusmanager.com/tutorials/ancm-diagnostics.html – Lex Li Oct 20 '20 at 21:55

2 Answers2

0

If you want to deploy ASP.Net Core MVC applications to IIS, you need to install the .NET Core host bundle, otherwise "500 Error" will appear. If you successfully install the .NET Core host bundle, you can find AspNetCoreModuleV2 in Modules:

enter image description here

Download the installer using the following link:

https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.9-windows-hosting-bundle-installer

After adding the site, you need set the .NET CLR version to No Managed Code:

enter image description here

For more information about using IIS to host ASP.NET Core on Windows, you can refer to this link:

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1

UPDATE:

I suggest you enable viewing of PII logs so that we can view more detailed information about the error:

public void ConfigureServices(IServiceCollection services)
{
    IdentityModelEventSource.ShowPII = true;
    ....

You can refer to this link, which contains similar questions:

"InvalidOperationException: IDX20803: Unable to obtain configuration from: '[PII is hidden]'"

Ding Peng
  • 3,702
  • 1
  • 5
  • 8
  • Thanks Ding! As I mentioned in my original post, I had the IIS hosting bundle installed already. And changing .Net CLR version to No Managed Code made no difference. One think I discovered however, after enabling server log, is that the error was actually caused by the following exception: "System.InvalidOperationException: IDX20803: Unable to obtain configuration from: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'." I have been searching for the cause of this exception and haven't had much of luck so far. – Alexu Oct 21 '20 at 18:58
  • Thanks Ding. I think my problem was caused by a failed db connection. The server log contains an error: "An error occurred using the connection to database 'MyTestIDS' on server '.'" and "Login failed for user '$'". I have a connection string defined in appsettings.json as "Server=.;Database=MyTestIDS;trusted_connection=True;" and it worked fine when selfhosted until now when hosted in IIS. I checked my deployed code and the appsettings.json is there. Does the app read configuration settings from appsettings.json under IIS? Where should I move it to if not and how? – Alexu Oct 22 '20 at 21:27
  • I have figured out the issue - the format of the connection string. When slfe-hosting, I used "Server=.;Database=MyTestDB;trusted_connection=True;" and it worked just fine there. But under IIS, I have to change it to something like "Server=MyServer,MyPort;Database=MyTestDB;user id=MyUsrID;Password=MyPassword". Not sure why but now I am working fine. – Alexu Oct 23 '20 at 19:57
0

For those with the same issue, I found that no setup guide I found mentions what was, for me, a required step:

  1. Open IIS Manager
  2. Navigate to [Server]\Sites[Your site]. A folder is listed for your .NET application, but the icon shows as an ordinary folder, as if in Windows Explorer.
  3. Right-click the folder and select, "Convert to Application".
Charles Burns
  • 10,310
  • 7
  • 64
  • 81