Asp.net mvc 4 Microsoft是否已更改ASP.NET MVC处理重复操作方法名称的方式?

Asp.net mvc 4 Microsoft是否已更改ASP.NET MVC处理重复操作方法名称的方式?,asp.net-mvc-4,Asp.net Mvc 4,我可能在这里遗漏了一些东西,但在ASP.NETMVC4中,我无法实现以下功能 给定以下控制器: public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string order1, string order2) { re

我可能在这里遗漏了一些东西,但在ASP.NETMVC4中,我无法实现以下功能

给定以下控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(string order1, string order2)
    {
        return null;
    }
}
它的观点是:

@{
    ViewBag.Title = "Home";
}

@using (Html.BeginForm())
{
    @Html.TextBox("order1")<br />
    @Html.TextBox("order2")
    <input type="submit" value="Save"/>
}
默认情况下,MVC 3应用程序会这样做。因此,我将上述内容添加到MVC4应用程序中,但由于相同的错误而失败。请注意,MVC 3应用程序确实可以很好地使用上述路线。我正在通过Request.Form传递“订单”数据

编辑: 在文件
RouteConfig.cs
中,我可以看到执行了
RegisterRoutes
,默认路由如下:

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

我仍然得到原始错误,即调用
Index()
方法之间的不确定性。

因为MVC4随ASP.Net Web.API一起提供,所以您可以潜在地引用两个
httpposattribute
(这同样适用于其他属性,如
HttpGet
等):

  • 由ASP.Net MVC使用,因此您需要在
    控制器
    派生控制器内的操作上使用它

  • 由ASP.Net Web.API使用,因此您需要在内部操作中使用它
    ApiController
    派生控制器

您在代码中已经引用了
System.Web.Http.httpposattribute
。将其更改为使用right属性,它应该可以正常工作:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [System.Web.Mvc.HttpPost]
    public ActionResult Index(string order1, string order2)
    {
        return null;
    }
}

您是否更改了默认路由?如果默认路由有两个可选参数,则无法区分这两个参数之间的差异。如果要重载操作名称,则需要使用
ActionName
属性。这里回答:@RayCheng-他没有以这种方式重载操作名称,他使用的是普通的HttpPost方法。Jason,你没有将该方法添加到Global.asax,现在App_Start中有一个名为RouteConfig.cs的文件,该方法是从Global调用的。asax@MystereMan,你说得对。谢谢你纠正我。
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [System.Web.Mvc.HttpPost]
    public ActionResult Index(string order1, string order2)
    {
        return null;
    }
}