我可能在这里遗漏了一些东西,但在ASP.NET MVC 4中,我无法使用以下内容.
给出以下控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string order1, string order2)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
它的观点是:
@{
ViewBag.Title = "Home";
}
@using (Html.BeginForm())
{
@Html.TextBox("order1")<br />
@Html.TextBox("order2")
<input type="submit" value="Save"/>
}
Run Code Online (Sandbox Code Playgroud)
启动应用程序时,我得到的只是:
控制器类型'HomeController'上的当前操作请求'Index'在以下操作方法之间是不明确的:System.Web.Mvc.ActionResult类型ViewData.Controllers.HomeController上的索引()System.Web.Mvc.ActionResult索引(系统. String,System.String),类型为ViewData.Controllers.HomeController
现在,在ASP.NET MVC 3中,上面工作正常,我只是尝试过,所以在ASP.NET MVC 4中有什么改变来打破这个?
好吧,我可能有机会在这里做些傻事,而不是注意到它.
编辑:
我注意到在MVC 4应用程序中,Global.asax.cs文件不包含这个:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,MVC 3应用程序.所以我将上面的内容添加到MVC 4应用程序中,但它失败并出现相同的错误.请注意,MVC 3应用程序可以正常使用上述路由.我通过Request.Form传递"订单"数据.
编辑:在RouteConfig.cs我可以看到的文件RegisterRoutes中执行,具有以下默认路由:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
我仍然得到原始错误,认为Index()调用哪种方法之间存在歧义.
nem*_*esv 10
因为MVC4附带ASP.Net Web.API,你可以引用两个HttpPostAttribute(同样适用于其他属性,如HttpGet,等):
System.Web.Mvc.HttpPostAttribute 由ASP.Net MVC使用,因此您需要在Controller派生控制器内的操作上使用它
System.Web.Http.HttpPostAttribute 由ASP.Net Web.API使用,因此您需要在ApiController派生控制器内的操作上使用它
您已System.Web.Http.HttpPostAttribute在代码中偶然引用.将其更改为使用正确的属性,它应该正常工作:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[System.Web.Mvc.HttpPost]
public ActionResult Index(string order1, string order2)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1486 次 |
| 最近记录: |