Why doesn't the Add New Item work for Razor/Blazor pages?

Falanga, Rod, DOH 190 Reputation points
2024-08-07T17:24:14.24+00:00

I was in Visual Studio 2022 and wanted to add a new Razor page to an ASP.NET Core application I'm writing using .NET 8. I press Contr-F2 to bring up the Add New Item dialog, then changed it from FileName.cs to Random.razor. That created it in the folder where I wanted to put it. Only problem is, it wouldn't work. I put the @page "/random" at the top of the page, then pressed F5 to get it started debugging. I put the "/random" at the end of the URL and got a HTTP 404 error. I removed all that I did. Then I thought I'd use VS 2022's right mouse-button click | Add | Razor Component and recreated everything I'd entered trying to use the Add New Item dialog. This time it worked fine.

Huh?

Why did it work using the older style way of adding a component, but the new Add New Item VS extension didn't work?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,096 questions
Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
214 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 30,561 Reputation points Microsoft Vendor
    2024-08-08T06:24:45.72+00:00

    Hello @Falanga, Rod, DOH,

    I think you mean "Shift-F2"(AddQuickFile), instead of "Contr-F2"(Window.MovetoNavigationBar) :D

    Creating a .razor file by using Shift-F2 is the same as right-clicking| Add | Razor Component. I’m curious, if you then try the Shift-F2 to create a new .razor file again, will the same error appear?

    You may check and confirm following things:

    1. Make sure that the .razor file is successfully created in the specified project, you set that project as the startup project and run that specific project, instead of another project.
    2. Consider that some cache files interfere VS. Try to clean your project/solution(manually remove bin, obj and the hidden .vs folder) and rebuild your project.
    3. From your description, I guess you were testing in an existing project which you are developing right? Did you try a newly created project and test again to confirm this issue?

    I tested with newly created ASP.NET Core (MVC) project, and follow this document: Add Blazor support to an ASP.NET Core app to integrate the component, also follow the steps that you mentioned in your original post, but failed to reproduce this issue.

    If you can reproduce this issue in a newly created project, please, maybe, kindly share us complete steps or a .gif picture(do not include any privacy information) to reproduce this issue and check further.

    Feel free to contact us.

    Tianyu


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.