简单的 .Net 5.0 网络应用程序获取 404(使用 mvc)

Simple .Net 5.0 web app getting 404s (using mvc)

.net新手看这里。我正在参加一个网络开发训练营,最近几天我遇到了一个持续存在的问题。我已经 运行 几个助教、其他学生和一些教师讲过它,但到目前为止我们都被难住了。

我在浏览器中打开 localhost:5000 页面或使用邮递员时收到 404。该项目是使用 dotnet new web --no-https -o ProjName 命令生成的,然后进行了编辑。

这是我的 Startup 的样子:

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options => options.EnableEndpointRouting = false);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseMvc();
        }
    }

这是我的控制器的样子

using Microsoft.AspNetCore.Mvc;

namespace Portfolio.Controllers
{
    class HomeController : Controller
    {
        [HttpGet("")]
        public string Index() => "This is my index";
    }
}

请注意,当我使用默认的自动生成代码但未使用此具有属性路由模式的控制器时,我会收到响应。如果你知道如何解决这个问题,那将是一个很大的帮助,因为我落后了。

我尝试过的事情:

我很乐意提供任何其他信息,而且我通常精通技术,可以跟进建议的内容。

谢谢

HttpGet 属性中指定一个空字符串就像指定 none.

您可以在useMvc方法中定义默认路由模式:

app.UseMvc(routes =>
{
    routes.MapRoute(
    name: "default",
    template: "{controller}/{action}/{id?}",
    defaults: new { controller = "Home", action = "Index" });
});

或者在HttpGet属性中设置一个斜杠来设置方法的根路径:

HttpGet("/")