gpt4 book ai didi

c# - HttpGet 在 Mvc 核心 2.0 中返回 404

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:14 25 4
gpt4 key购买 nike

public class SurveyController : Controller
{
public IActionResult Index()
{
var surveys = new List<int>{1};
return View(Surveys);
}

[HttpGet("conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View("Conditions",Conditions);
}
}

现在浏览量低于

Views/Sur​​vey/Index.cshtml

Views/Sur​​vey/Conditions.cshtml

路线 Survey/conditions 正在返回 404

有人知道吗?

我的 startup.cs 是 -

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseAuthentication();

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

最佳答案

你对 Action 的属性:

[HttpGet("conditions")]

指定您希望 URL 为 http://whatever/conditions。相反,你应该使用:

[HttpGet("/survey/conditions")]

如果您希望以更好的粒度控制路由,则应改用 Route 属性。例如:

[Route("[controller]")] //Set the prefix for subsequent route attributes
public class SurveyController : Controller
{
[Route("conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View("Conditions",Conditions);
}
}

引用 Routing to Controller Actions

关于c# - HttpGet 在 Mvc 核心 2.0 中返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48384406/

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