【问题标题】:HTTP ERROR 405 | ASP.NET Core 3.1 MVC | slash problemHTTP 错误 405 | ASP.NET Core 3.1 MVC |斜线问题
【发布时间】:2021-05-25 18:40:07
【问题描述】:

我需要一些帮助。

我正在使用 ASP.NET Core 3.1 MVC,一切顺利,但不是针对特定操作。

我有几个页面:开始 -> 索引 -> 添加用户。

感谢<button>s,我可以在我的页面之间导航。

Index.cshtml

<form asp-controller="Home" asp-action="AddUser" asp-route-GB="@ViewBag.GB">
      <button type="submit" class="btn btn-success">Add</button>
</form>

HomeController.cs

[HttpGet]
public ActionResult Start(string GB) {
  if (string.IsNullOrEmpty(GB)) {
    return RedirectPermanent("Error");
  }
  else {
    return View();
  }
}
[HttpGet]
public ActionResult Index(string GB) {
  if (string.IsNullOrEmpty(GB)) {
    return RedirectPermanent("Error");
  }
  else {
    return View();
  }
}
[HttpGet]
public ActionResult AddUser(string GB) {
  if (string.IsNullOrEmpty(GB)) {
    return RedirectPermanent("Error");
  }
  else {
    return View();
  }
}
public ActionResult Error() {
  return View();
}

网址结果:

https://localhost:XXXXX/Home/Start?GB=1 -> 进展顺利。

https://localhost:XXXXX/Home/Index?GB=1 -> 进展顺利。

https://localhost:XXXXX/Home/AddUser?GB=1 -> HTTP 错误 405

但是!如果我在最后一个斜杠上添加一个斜线: https://localhost:XXXXX/Home/AddUser/?GB=1 -> 进展顺利。

发生了什么事?我不想在我的浏览器上添加斜线以使其正常工作...

【问题讨论】:

  • 您能展示一下您的 AddUser 操作吗?
  • 您有多个 AddUser* 路由吗?如果是这样,请尝试重命名路由名称并检查路由器无法正确识别此路由的原因。如果您还没有这样做,您可以通过添加路由属性 [HttpGet("AddUser?GB={gb})]" 使其更加明确。但是,根据提供的信息,无法回答为什么路线不是唯一匹配到操作的原因。
  • 现在你得到了我的整个 HomeController.cs :)

标签: asp.net-mvc asp.net-core-mvc asp.net-core-3.1 http-status-code-405 slash


【解决方案1】:

添加或创建 api 的第一件事,它必须是 POST 类型而不是 GET。 尝试使用[HttpPost] 属性。它可能会解决您的问题。

【讨论】:

  • 嗯,实际上,它确实有效。但我不明白,我不是在“发布”任何东西,我只是想获得一个页面。就像索引或开始...
【解决方案2】:

好的,我明白了,基本上

  • &lt;a&gt;支持[HttpGet]
  • &lt;button&gt; + &lt;form&gt; 支持[HttpPost]

如果你想使用[HttpGet] 那么:

<a class="btn btn-success" asp-controller="Home" asp-action="AddUser" asp-route-GB="@ViewBag.GB">
    Next Step
</a>

如果你想使用[HttpPost] 那么:

<form asp-controller="Home" asp-action="AddUser" asp-route-GB="@ViewBag.GB">
    <button type="submit" class="btn btn-success">Next Step</button>
</form>

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 2021-03-27
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2020-07-06
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多