gpt4 book ai didi

c# - 如何在 ASP Core 中使用 HttpGet 指定默认 View

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:27 24 4
gpt4 key购买 nike

我正在尝试从 Startup.cs(如下)调用 View 。 Controller 称为 AppController,方法是 Index。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=App}/{action=Index}");
});
}

当我启动我的应用程序 ( http://localhost:8000/ ) 时,我收到 404 错误。我以为上面我设置了默认值以映射到 AppController.cs 中的 Index 方法。我认为问题是在我的索引 Controller 上我有一个 HttpGet 因为我有一个要在该页面上提交的表单(正在运行)

[HttpGet("index")]
public IActionResult Index()
{
return View();
}

当我转到 http://localhost:8000/index 时,索引页面按预期显示.我在 Configure 中做错了什么吗?

最佳答案

你应该只使用:

[HttpGet("")]

或者只是

[HttpGet]

代替

[HttpGet("index")]

HttpGet 属性需要一个模板,因此通过将 string 添加到此属性,您表示仅当相对 url 为“/Index”时才会触发您的操作,因此使用绝对 URL“http://localhost:8000/index”。

如果您希望默认路由按照您配置的那样工作,那么您应该删除属性中的 string

关于c# - 如何在 ASP Core 中使用 HttpGet 指定默认 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51071420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com