c# - 获得 404 的简单 .Net 5.0 Web 应用程序(使用 mvc)
问题描述
.Net 新手在这里。我正在关注一个 Web 开发训练营,过去几天我遇到了一个持续存在的问题。我已经由几个助教、其他学生和一些教练运行过它,但到目前为止我们都被难住了。
在浏览器中打开 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";
}
}
请注意,我在使用默认自动生成代码但未将此控制器与属性路由模式一起使用时收到响应。如果您知道如何解决这个问题,那将是一个很大的帮助,因为我落后了。
我尝试过的事情:
- 卸载与 .net 相关的所有内容并安装单个版本(目前为 5.0.100,但我也尝试了其他一些版本)
- 卸载并重新安装 IIS Express
- 尝试从另一台计算机运行项目。没运气。
- 更改端口。没运气。
- 使用不同的浏览器。没运气。
- 在我的键盘上抽泣。没运气。
- 重做同一个项目十几次。
我很乐意提供任何其他信息,而且我通常精通技术,可以跟进建议的内容。
谢谢
解决方案
在属性中指定空字符串HttpGet
就像指定无。
您可以在方法中定义默认路由模式useMvc
:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
或者在属性中设置一个斜线HttpGet
来设置方法的根路径:
HttpGet("/")
推荐阅读
- node.js - 如何使用 vb.net 客户端连接到 node.js Websocket 服务器
- c - 在 C 中解密密码
- c# - 我不能在注册表中添加超过 10 个数字值 DWord
- django - 获取 django.contrib.staticfiles.views.serve 引发的异常
- legend - 删除饼图图例中名称和百分比之间的空格 (amcharts4)
- cuda - 如何对 CUDA 内核中的结构进行原子操作?
- c# - 闪烁的文本框
- c - 如何进入路径中的上一个文件夹?
- javascript - html到内容管理系统
- python - 当类存储在熊猫数据框中时,如何矢量化以访问类方法而不是应用?