【问题标题】:AmbiguousMatchException: Request for action is ambiguousAmbiguousMatchException:操作请求不明确
【发布时间】:2018-06-07 08:53:55
【问题描述】:

编辑 - 问题解决了!

System.Web.Mvc 的程序集引用尚未通过升级更新。

Web.config:

Broken:
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.1" />
  </dependentAssembly>
Fixed:
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
  </dependentAssembly>

/编辑

我正在运行一个 DNN 网站 + 网上商店(Hotcakes),最近升级了 [DNN 7.4.2 -> 8.0.3,HCC 1.10.4 Pro -> 03.02.00]。

我设法解决了所有问题,但只有一个问题:

“System.Reflection.AmbiguousMatchException:当前对控制器类型“CartController”的操作“Index”的请求在以下操作方法之间不明确: System.Web.Mvc.ActionResult IndexPost() on type Hotcakes.Modules.Core.Controllers.CartController Hotcakes.Modules.Core.Controllers.CartController 类型上的 System.Web.Mvc.ActionResult Index()

我找到了各种问题,包括Routing: The current request for action [...] is ambiguous between the following action methods,但没有找到解决方案。

根据我了解到的情况,这应该没问题 - 相同的 ActionName,一个 Get,一个 Post。没有称为 Index 或 IndexPost 的附加方法。 我错过了什么? 另外,我无法使用 MapRoutes,因为 DNN 正在控制它 -> 没有 Global.asax.cs。

动作方法:

    #region Main Cart Actions

    // GET: /Cart/
    [NonCacheableResponseFilter]
    [HccHttpGet]
    public ActionResult Index()
    {
        var model = IndexSetup();
        HandleActionParams();
        CheckForQuickAdd();
        LoadCart(model);
        ValidateOrderCoupons();
        CheckFreeItems(model);

        if (ModuleContext != null && ModuleContext.Configuration.DesktopModule.ModuleName == "Hotcakes.Cart")
            HccApp.AnalyticsService.RegisterEvent(HccApp.CurrentCustomerId, ActionTypes.GoToCart, null);

        CheckForStockOnItems(model);
        return View(model);
    }

    // POST: /Cart/
    [ActionName("Index")]
    [HccHttpPost]
    public ActionResult IndexPost()
    {
        var model = IndexSetup();
        LoadCart(model);

        var intResult = CartIntegration.Create(HccApp).BeforeProceedToCheckout(HccApp, model);

        if (!intResult.IsAborted)
        {
            if (CheckForStockOnItems(model))
            {
                ForwardToCheckout(model);
            }
        }
        else
        {
            FlashWarning(intResult.AbortMessage);
        }

        return View(model);
    }

观点:

namespace Hotcakes.Modules.MiniCart
{
    public partial class MiniCartView : HotcakesModuleBase
    {
        protected override string RenderView()
        {
            var viewName = Convert.ToString(Settings["View"]);
            if (!string.IsNullOrEmpty(viewName))
                return MvcRenderingEngine.Render("Cart", "Index");
            return MvcRenderingEngine.Render("Cart", "Index", "MiniCart", new {MiniCart = true});
        }
    }
}

帖子属性:

namespace Hotcakes.Commerce.Extensions
{
    [AttributeUsage(AttributeTargets.Method)]
    public sealed class HccHttpPostAttribute : ActionMethodSelectorAttribute
    {
        // Fields
        private static readonly AcceptVerbsAttribute _innerAttribute = new AcceptVerbsAttribute(HttpVerbs.Post);

        // Methods
        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            var hccRequestType = (string) controllerContext.RequestContext.RouteData.Values["hccrequesttype"];
            if (Factory.CreateHccFormRenderer().VirtualFormUsed && !string.IsNullOrEmpty(hccRequestType))
            {
                return hccRequestType == "hccpost";
            }
            return _innerAttribute.IsValidForRequest(controllerContext, methodInfo);
        }
    }
}

【问题讨论】:

  • Hotcakes 的全新安装不会出现这种情况,所以听起来您可能修改了源代码?我不建议对任何开源解决方案这样做。 :)
  • 源代码没有被修改——我只是为了解决这个问题下载了源代码。我将尝试设置全新安装,我想升级时出了点问题。

标签: c# asp.net-mvc dotnetnuke


【解决方案1】:

你有两个同名的方法,因为你用 [ActionName("Index")] 修饰了 IndexPOST,所以现在它们都有路由 /YourController/Index。

要么改变属性值,要么改变方法名Index()(第一个)

【讨论】:

  • Afaik 这应该不是问题,因为您可以在控制器上拥有 2 个同名的操作方法,只要一个是 [HttpPost],另一个是 [HttpGet]
  • @AloisKrichmayr 是的,您可以使用相同的名称,但不能使用相同的签名! (名称和参数编号)
猜你喜欢
  • 2017-06-30
  • 2012-05-26
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多