gpt4 book ai didi

asp.net - 即使使用 [HttpPost] 属性, Controller 操作也含糊不清? (ASP.NET MVC4)

转载 作者:行者123 更新时间:2023-12-05 04:17:22 24 4
gpt4 key购买 nike

所以我有一个“Register” View (映射到 RegisterController),这是一个简单的表单,供人们在我的服务中注册。鉴于表单的方法是 POST,那么我可以将 Controller 的两个 Action (显示表单的 Action 和接收提交的 Action )命名为相同的名称,但通过 [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

这非常有效。

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

 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

最佳答案

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

关于asp.net - 即使使用 [HttpPost] 属性, Controller 操作也含糊不清? (ASP.NET MVC4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23153823/

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