【发布时间】:2020-01-07 19:26:39
【问题描述】:
我有一个不断弹出的奇怪错误,通常是在调试时,我会得到一个路由突然抛出这个异常,并且会继续那个路由,直到我停止调试。奇怪的是我只有一个可以匹配的动作,只有一个具有该名称的动作,只有控制器等。
我的问题是,有没有人遇到过这种奇怪的行为?有什么修复吗?在修复安装 Visual Studio 2019、重新启动等后,这种情况仍然存在。我认为这是项目的问题,但我无法通过谷歌搜索找到任何东西,我什至不知道从哪里开始寻找。它不可能与 RouteConfig.cs 和属性路由中的后备路由匹配,对吧?
更新:即使遵循清理 routeconfig.cs 中类似路由的建议,也会发生在单独的控制器(另一个属性路由控制器)上。下面添加了详细信息。
当前请求在以下操作方法之间存在歧义:
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) 类型 >Origin2.Controllers.Events2.Events2APIController
修剪下来的控制器:
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");
}
}
}
精简了 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" }
);
}
}
}
修复 routeconfig.cs 的模糊路由后。
当前请求在以下操作方法之间存在歧义:
Origin2.Controllers.Events2.Events2Controller 类型上的 System.Web.Mvc.ActionResult EventLanding(System.String)
Origin2.Controllers.Events2.Events2Controller 类型上的 System.Web.Mvc.ActionResult EventLanding(System.String)
精简了 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");
}
}
}
【问题讨论】:
-
通过反射找到控制器;您的 bin 文件夹中是否有另一个(可能现在已过时/重命名)程序集,其中包含相同的控制器?
-
你能解释一下你使用的哪个路由导致了这个错误,并请给我们看看 route.config。
-
这是 'localhost:44326/app/api/Events/aaaba/questions/get' 的 POST,添加了一个精简的路由配置。
-
@sellotape,我挖了一点,只找到了大量的 .net 程序集和这个应用程序的程序集,以及 obj 文件夹中的副本,在 ildasm 中一切看起来都很好......
-
@ChrisWhipple 在这里猜测一下,路由 DefaultApp 和/或 DefaultApp2 可能与 AttributeRouting 发生冲突。
标签: c# asp.net asp.net-mvc asp.net-mvc-5 visual-studio-2019