.NET 6 Core Web App Returns web page not found

Jacaru Tech 31 Reputation points
2022-03-13T22:33:29.183+00:00

I am copying over from Stack:

I have searched around here and other places to figure out the issue, but I can't find a good solution. I created a new .NET Core Web App targeting .NET6. I created the project without authentication and without Docker support. I would expect a new app to run out-of-the-box with the default landing page, but instead I get a webpage not found error. It's not hitting the Index OnGet method for the Razor page. This is a simple app that I only intend on running myself, so I won't be using MVC, only Razor. Has anyone solved this in .NET 6? Here is the response, Program class, and launchSettings:

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // 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.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

launchSettings

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:30529",
      "sslPort": 44345
    }
  },
  "profiles": {
    "FinanceTracker": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:7137;http://localhost:5137",
      "dotnetRunMessages": true
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

EDIT: New information...

It continued to error until I did the following:

run the project from the console (dotnet run)
Navigate to the expected url
Close
Click 'Start without Debugging'
App opens
Close
It will now allow me to run from debugging
Something funky going on with the template for NET6

EDIT2:

I have to run dotnet run every time I make a code change. Otherwise I get the same error when starting the program.

EDIT3:

If I run dotnet build from the CLI, it will run in debug mode after. If I just rebuild from VS, it will not. If I change code, I have to run dotnet build before running in debug mode.

As mentioned as a reply to a comment / answer below, It's not a port issue. I've created a mirror project in NET5 in VS2019 with the same port, and it works fine. Trying NET6 from VS 2022 does not work so well. Maybe I have a misconfigured setting in VS 2022?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,612 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
{count} vote

6 answers

Sort by: Most helpful
  1. Owen Mortensen 41 Reputation points
    2022-10-20T18:18:09.173+00:00

    Found it!
    For me, this is what the issue is whenever I try to add a razor page to an existing project:

    In VisualStudio, highlight the cshtml file in the SolutionExplorer and then go to "Properties" and change the "Build Action" from "None" to "Content"!
    I have not frigging idea why "Content" is not the default for a new Razor page as having it set to "none" will guarantee that you will get a 404 error!

    Cheers

    7 people found this answer helpful.

  2. LDTackett 15 Reputation points
    2023-08-11T19:30:49.3933333+00:00

    I was having this same issue and none of this worked. I finally went to update my VS through the installer and fixed it. Go to VS Installer and click Modify. Under "ASP.NET and web development" make sure you have ".Net WebAssembly Build Tools" and ".NET 6.0 WebAssembly Build Tools." I'm not sure why I didn't have these before but this worked for me.

    User's image

    User's image

    3 people found this answer helpful.

  3. Jacaru Tech 31 Reputation points
    2022-03-20T13:05:34.737+00:00

    This was the answer. I updated VS 2022 and the problem resolved. I'm sure repair would have worked, as well.

    ZhiLv-MSFT JacaruTech-8119 · 5 days ago
    Can't reproduce the problem on my side, I'm using the VS 2022 17.1.0 version for windows 10 Development.

    Before click the start debug button, have you ever save the changes and build the project? Try to right click the project and build the application first, then click the start button to debug the project, whether this issue disappear?

    Besides, can you tell us the detailed VS version? Not sure if the issue relates the VS, but you can try to repair the VS vis the Visual Studio Installer and check it.

    2 people found this answer helpful.

  4. Zhi Lv - MSFT 32,446 Reputation points Microsoft Vendor
    2022-03-14T06:07:21.043+00:00

    Hi @Jacaru Tech ,

    From your description, it seems that you are using VS2022 to create an Asp.net core Web application (a Razor page application), then use the .NET CLI command to run the application, right? If that is the case, it will not trigger the debugger, and after modifying the code, you have to re-build the application and run the application, then the updated content will work. It is because that you are directly running the application's exe file.

    You have to use the Visual Studio 2022 debugger to debug the code. If you want to use the .Net CLI commands, you might need to use Visual Studio code, refer Tutorial: Debug a .NET console application using Visual Studio Code. In my opinion, I prefer to use VS 2022 debugger.

    You can refer the following steps to create a asp.net core web application and debug it using Visual Studio 2022.

    1. Create a new Asp.net core Web App.
    2. In Solution Explorer, open Index.cshtml.cs (under Pages/Index.cshtml) and add a break point.
      image 1
    3. Then, select the Debug mode, and run the application on IIS express.
      182610-image.png

    The result as below:

    182691-2.gif

    More detail information about debug, see What is debugging? and Quickstart: Debug ASP.NET Core with the Visual Studio debugger.

    Besides, in VS 2022, there is a new feature about .NET Hot Reload, with Hot Reload you can now modify your apps managed source code while the application is running, without the need to manually pause or hit a breakpoint. You can also refer the above .gif image to see the result.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


  5. KC746555124 1 Reputation point
    2022-09-29T21:14:48.047+00:00

    I am using Visual Studio 2022 17.4.0 Preview 2.1 and I got the same error when I attempt to run in .net 6 or .net 7. I don't have any problem when I run in .net 5.

    246130-image.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.