简体   繁体   English

ASP.NET MVC5 属性路由 - 只有一个匹配操作的模糊操作异常

[英]ASP.NET MVC5 Attribute routing - Ambiguous action exception with only one matching action

I have a weird bug that keeps popping up, usually while debugging, where I will get one route throwing this exception out of nowhere, and will continue for that route until I stop debugging.我有一个奇怪的错误,通常在调试时不断弹出,在那里我会得到一条路由,从任何地方抛出这个异常,并将继续该路由,直到我停止调试。 Weird thing is I only have one action that would ever match, and only one action with that name, only controller, etc.奇怪的是我只有一个可以匹配的动作,并且只有一个具有该名称的动作,只有控制器等。

My question is, has anyone come across such weird behavior?我的问题是,有没有人遇到过这种奇怪的行为? Any fixes?任何修复? This has persisted after a repair install of Visual Studio 2019, reboots, etc. I think its an issue with the project but I can't find anything with a google search and I have no clue where to even begin looking.在修复安装 Visual Studio 2019、重新启动等之后,这种情况仍然存在。我认为这是项目的一个问题,但我无法通过谷歌搜索找到任何内容,我什至不知道从哪里开始寻找。 There's no way it's matching against the fallback route in the RouteConfig.cs AND the attribute route, right?它不可能与 RouteConfig.cs 和属性路由中的回退路由匹配,对吗?

Update: Happened even following suggestions to clean up the similar routes in routeconfig.cs, happening on a seperate controller (another attribute routed controller).更新:甚至按照建议清理 routeconfig.cs 中的类似路由,发生在单独的控制器(另一个属性路由控制器)上。 Details added below.详情如下。

The current request is ambiguous between the following action methods:当前请求在以下操作方法之间存在歧义:

System.Web.Mvc.ActionResult Getquestions(System.String, System.String, System.Web.Mvc.FormCollection) on type >Origin2.Controllers.Events2.Events2APIController System.Web.Mvc.ActionResult Getquestions(System.String, System.String, System.Web.Mvc.FormCollection) 类型 >Origin2.Controllers.Events2.Events2APIController

System.Web.Mvc.ActionResult Getquestions(System.String, System.String, System.Web.Mvc.FormCollection) on type >Origin2.Controllers.Events2.Events2APIController System.Web.Mvc.ActionResult Getquestions(System.String, System.String, System.Web.Mvc.FormCollection) 类型 >Origin2.Controllers.Events2.Events2APIController

Trimmed down controller:修剪控制器:

using ...


namespace Origin2.Controllers.Events2
{

    [RoutePrefix("app/api/Events")]


    public class Events2APIController : Controller
    {

        [Route("{code}/questions/GET")]
        [HttpPost]
        public ActionResult Getquestions(string code, string mode, FormCollection collection)
        {
            //CODE LIVES HERE. 
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            ExceptionNotifications.SendNotice(filterContext, null, "Events2 API Controller");
        }


    }

}

Trimmed down routeconfig.cs删减了 routeconfig.cs

using Origin2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Origin2
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes(); //Attribute Routing Enabled


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

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

            routes.MapRoute(
                name: "app2",
                url: "app/{action}",
                defaults: new { controller = "app", action = "Index" }
            );

            routes.MapRoute(
                name: "DefaultApp",
                url: "app/{controller}/{action}/{**id}",
                defaults: new { controller = "app", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "DefaultApp2",
                url: "app/{controller}/{action}",
                defaults: new { controller = "app", action = "Index" }
            );
            routes.MapRoute(
                name: "DebugShorthand",
                url: "debug",
                defaults: new { controller = "app", action = "Debug" }
            );
            routes.MapRoute(
                name: "RootAddress",
                url: "",
                defaults: new { controller = "app", action = "Index" }
            );
            routes.MapRoute(
                name: "DefaultOrigin1",
                url: "app/{*url}",
                defaults: new { controller = "app", action = "SendToOrigin1" }
            );
        }
    }
}

After fixing routeconfig.cs's ambiguous route.修复 routeconfig.cs 的歧义路由后。

The current request is ambiguous between the following action methods:当前请求在以下操作方法之间存在歧义:

System.Web.Mvc.ActionResult EventLanding(System.String) on type Origin2.Controllers.Events2.Events2Controller System.Web.Mvc.ActionResult EventLanding(System.String) 类型 Origin2.Controllers.Events2.Events2Controller

System.Web.Mvc.ActionResult EventLanding(System.String) on type Origin2.Controllers.Events2.Events2Controller System.Web.Mvc.ActionResult EventLanding(System.String) 类型 Origin2.Controllers.Events2.Events2Controller

Trimmed down Events2Controller:精简了 Events2Controller:

using Origin2.Models;
using Origin2.Models.Events2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using static Origin2.Models.Events2.EventRoles;

namespace Origin2.Controllers.Events2
{
   [RoutePrefix("app/Events")]
   [Route("{action=index}")] 

  public class Events2Controller : Controller
    {

        [Route("{code}")]
        public ActionResult EventLanding(string code)
        {
            //This will be the public facing thank you handler for all signup activities. 
            if (code.ToUpper() == "AAABD") 
            {
                return View("EventLandingCustom_AAABD");
            }
            return View();
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            ExceptionNotifications.SendNotice(filterContext, null, "Events2 Controller");

            // Redirect on error:
            //filterContext.Result = RedirectToAction("Error", "Index");
        }
   }
}

I think these two routes are identical我认为这两条路线是相同的

routes.MapRoute(
            name: "DefaultApp",
            url: "app/{controller}/{action}/{**id}",
            defaults: new { controller = "app", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "DefaultApp2",
            url: "app/{controller}/{action}",
            defaults: new { controller = "app", action = "Index" }

try commenting out one of these.尝试注释掉其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM