gpt4 book ai didi

c# - MVC 属性路由 - 使用 GET 和 POST 的默认 Controller 索引

转载 作者:太空狗 更新时间:2023-10-29 22:26:32 24 4
gpt4 key购买 nike

我们有一个 MVC 5.1 项目并且正在使用属性路由。一切正常,除了默认页面上有一个登录表单。

[RoutePrefix("Home")] 
public class HomeController : BaseController
{
[Route("~/")]
[Route]
[Route("Index")]
[HttpGet]
public ActionResult Index()
{
var model = new LoginViewModel();

return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(String Username, String Password)

表单通过 GET 正常显示,但在 POST 时我们得到...

HTTP 错误 405.0 - 方法不允许

您要查找的页面无法显示,因为使用了无效的方法(HTTP 动词)。

通常默认路由可以很好地处理 POST 和 GET。

   routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{dealerId}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

显然,我在默认路由上的帖子路由中遗漏了一些东西,因为其他页面上的后续帖子工作正常。

有人做过吗?

谢谢,

最佳答案

好吧,看来我所要做的就是添加

 [Route("~/")]
[Route]
[Route("Index")]
[HttpPost]
[ValidateAntiForgeryToken]

public ActionResult Index(String Username, String Password)

真的很明显!漫长的一天!

关于c# - MVC 属性路由 - 使用 GET 和 POST 的默认 Controller 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631520/

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