1

I have a problem with ASP.NET Core MVC or Razor applaunch.json.

I use a default template to create a project with default settings:

  • ASP.Net Core Web App - Dot Net 6
  • ASP.Net Core Web App MVC - Dot Net 6

This is my applaunch.json:

{
    "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
            "applicationUrl": "http://localhost:18583",
            "sslPort": 44326
        }
    },
    "profiles": {
        "WebApplication12": {
            "commandName": "Project",
            "dotnetRunMessages": true,
            "launchBrowser": true,
            "applicationUrl": "https://localhost:7081;http://localhost:5081",
            "environmentVariables": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            }
        },
        "IIS Express": {
            "commandName": "IISExpress",
            "launchBrowser": true,
            "environmentVariables": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            }
        }
    }
}

And program.cs :

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

After launch console shows that no problem with listening to ports :

Console after launch

But I get an exception for my ASP.NET Core MVC projects:

When running MVC projects

And I get 404 for Razor Pages:

When running Razor Page projects

All configurations are default template configurations.

Did anyone experience the same problem?

3
  • 1
    launch - not "lunch" ..... Commented Apr 16, 2022 at 16:55
  • 1
    In the same directory of the project, do dotnet run watch and see if that helps. I had a similar problem and the root cause might be Visual Studio did not attempt or its launch of dotnet run got killed. Commented Apr 16, 2022 at 17:54
  • @devyJava It's working with dotnet run watch but launching with Visual Studio not working and it's really annoying. Commented Apr 16, 2022 at 18:04

1 Answer 1

5

I solved the problem by using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package and adding the following service:

For ASP.NET Core Razor template

builder.Services.AddRazorPages().AddRazorRuntimeCompilation();

And for the MVC project template:

builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();

The second solution is to use dotnet run watch either in Developer Power Shell or Command-Line.

Sign up to request clarification or add additional context in comments.

4 Comments

This solved my problem also, adding the AddRazorRuntimeCompilation() to AddControllersWithView(). Trying to fully understand why it worked though
@Isaac Yes it works, yet I could not manage to understand what is going on under the hood; please share if you find something :)
Solved my problem too. But I have no idea why the default template is not working by default.
This issue still seems to persist in .NET 8 templates. Thanks for the hint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.