ASP.NET核心默认模板项目因HTTP错误404或异常而失败

发布于 2025-01-21 18:05:56 字数 2228 浏览 0 评论 0原文

我对ASP.NET Core MVC或Razor Applaunch.json有问题。

我使用默认模板来创建一个具有默认设置的项目:

  • ASP.NET Core Web应用程序-DOT NET 6
  • ASP.NET CORE WEB APP MVC -DOT NET 6

这是我的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"
            }
        }
    }
}

和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();

启动控制台后表明听端口的毫无问题:

“启动后的控制台”

但我对我的ASP.NET核心MVC项目有一个例外:

“运行MVC

我得到404的Razor页面:

“

所有配置都是默认模板配置。

有人遇到同样的问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尴尬癌患者 2025-01-28 18:05:56

我通过使用microsoft.aspnetcore.mvc.razor.runtimecompilation软件包解决了问题,并添加以下服务:

for asp.net core剃须刀模板

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

和MVC项目模板:

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

第二个解决方案是使用<<代码> dotnet运行手表在开发人员电源外壳或命令行中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文