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

标签 c# asp.net-mvc-5 asp.net-mvc-routing asp.net-mvc-5.1 attributerouting

我们有一个 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/

相关文章:

asp.net-mvc - ASP.Net Core 中的动态路由

.net - 如何在同一个 IIS 网站下合并 MVC4 站点和 WebAPI 站点?

c# - 需要澄清 Thread.MemoryBarrier()

c# - WCF 服务客户端 : The content type text/html; charset=utf-8 of the response message does not match the content type of the binding

c# - Linq 查询不返回值

c# - SqlDataReader 与数据集

ssl - 将 SSL MVC 5 应用程序部署到共享主机 - 403 错误

asp.net-mvc - 我可以在这里使用路线约束吗?

c# - TextBlock 中文本的水平自动滚动

c# - 使用 C# 制作自解压可执行文件