.NET 5 webapi example returning 404, running in Linux

Viewed 735

I'm trying to use .NET 5.0 on Linux, using VS Code.

I've created an empty directory, ran "code ." in it to open it up in VS Code, then created the sample project with:

dotnet new webapi -n Experimenting

When the "Required assets to build and debug are missing from 'experiments'. Add them?" dialog appeared, I clicked "Yes".

I ran the build task, then selected "Start Debugging" from the "Run" menu.

The project appears to run, and it opened up a Chrome browser tab to https://localhost:5001/, and the tab displays HTTP ERROR 404.

The "Expementing" profile in lauchSettings.json has applicationUrl set to "https://localhost:5001;http://localhost:5000", and the debug console includes:

Now listening on: https://localhost:5001 Now listening on: http://localhost:5000

So the ports seem to be correct.

If I try to open a browser tab on http://localhost:5000, it redirects to https://localhost:5001, and then shows the 404.

What am I missing?


The comment by jmoerdyk is correct - if I try https://localhost:5001/swagger, I get the swagger page.

In .NET Framework, Web API creates a landing page at the domain root. .NET Core does not.

The question, then, is why is the browser tab opening up at https://localhost:5001, and not at https://localhost:5001/swagger?

I had noticed this in launchSettings.json:

"profiles": {
    "IIS Express": {
        ...
    },
    "Experimenting": {
        "commandName": "Project",
        "dotnetRunMessages": "true",
        "launchBrowser": true,
        "launchUrl": "swagger",
        "applicationUrl": "https://localhost:5001;http://localhost:5000",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
}

And I thought that should open up the browser pointing to the swagger page. Browsing around I've seen statements that that launchSettings.json is not used by VS Code, but it clearly is, because if I change ports in applicationUrl, I the swagger page running on the new ports.

But launchUrl seems to be ignored.

Browsing around the web gives me the same sort of wrong and outdated answers that is typical for .NET Core questions, but I eventually tried setting the uriFormat in launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            ...
            // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\\bNow listening on:\\s+(https?://\\S+)",
                "uriFormat": "%s/swagger"
            },
            ...
        },
    ]
}

The link in the comment explains how this is supposed to work - kinda.

https://aka.ms/VSCode-CS-LaunchJson-WebBrowser

1 Answers

The reason for 404 code that there is no defautl GET request for root as you figured out already.

Based on this record, it tells that launchSettings.json is only for Visual Studio debug. I tried project on Windows with VS and on Linux with dotnet CLI, I can confirm this experience.

Is launchSettings.json used when running ASP.NET 5 apps from the command line on Mac?

You can use a redirect as alternative way for route to get Swagger as default instead of launchSettings.json file. You can create this class (for example as RouteRedirectController.cs) onto Controllers folder and it will redirect to swagger in case of a root GET call:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace TestAPI.Controllers
{
    [ApiController]
    [Route("")]
    public class RouteRedirectController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Redirect("~/swagger");
        }
    }
}

Update: To be honest, it is a bit strange because if I change "applicationUrl" in launchSettings.json file (not for IIS Express profile that is for Windows), then change is reflected after dotnet run command is executed. Ignore launchUrl maybe a bug during dotnet run? But if it does anyway, then it can be resolved by this redirect endpoint.

Related