即使使用[HttpPost]属性,控制器操作也会模糊不清? (ASP.NET MVC4)

时间:2014-04-18 12:09:36

标签: asp.net asp.net-mvc asp.net-mvc-4 f#

所以我有一个“注册”视图(映射到RegisterController),这是人们在我的服务中注册的简单形式。鉴于表单的方法是POST,那么我可以将控制器的两个操作(显示表单的操作和接收其提交的操作)命名为相同的名称,但是通过[HttpPost]属性区分它们,方式:

open System
open System.Web
open System.Web.Mvc
open System.Net
open System.Threading
open System.Configuration

[<HandleError>]
type RegisterController() =
    inherit Controller()

    member this.Index (guid: string) =
        let model = new RegisterModel(guid)
        this.View("Register", model) :> ActionResult

    [<HttpPost>]
    member this.Index
        (guid: string, password: string, passwordRetry: string) =
            if not (password.Equals(passwordRetry)) then
                raise(new Exception("Passwords must be the same"))
            WebDbAccess.SetNewPassword guid password
            RedirectResult("/") :> ActionResult

这很有效。

但是,我只是尝试在我的应用程序的主页视图中使用相同的技术(对于登录功能),它不起作用!它抛出了这个:

 The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String, System.String) on type FsWeb.Controllers.HomeController
System.Web.Mvc.ActionResult Index() on type FsWeb.Controllers.HomeController
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Reflection.AmbiguousMatchException: The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String, System.String) on type FsWeb.Controllers.HomeController
System.Web.Mvc.ActionResult Index() on type FsWeb.Controllers.HomeController

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[AmbiguousMatchException: The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String, System.String) on type FsWeb.Controllers.HomeController
System.Web.Mvc.ActionResult Index() on type FsWeb.Controllers.HomeController]
   System.Web.Mvc.Async.AsyncActionMethodSelector.FindAction(ControllerContext controllerContext, String actionName) +495852
   System.Web.Mvc.Async.ReflectedAsyncControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName) +57
   System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName) +16
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +114
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +382
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +249
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929 

两种情况有什么区别?我不明白。

HomeController的实现是:

open System
open System.Web
open System.Web.Http

[<HandleError>]
type HomeController() =
    inherit Controller()

    [<HttpGet>]
    member this.Index () =
        this.View() :> ActionResult

    [<HttpPost>]
    member this.Index
        (id:string, password:string) =
            if (UserIsValid id password) then
                RedirectResult("lobby") :> ActionResult
            RedirectResult("loginError") :> ActionResult

1 个答案:

答案 0 :(得分:1)

@nemesv是对的,我必须使用HttpPost完全限定System.Web.Mvc.HttpPost,否则会使用System.Web.Http.HttpPost !!