简体   繁体   English

覆盖不适用于ASP.net Core MVC中的虚拟方法

[英]Overriding does not work on virtual method in ASP.net Core MVC

namespace ProjectA.Controllers
public partial class CustomerController : Controller
{
 public virtual IActionResult Index()
 {
  ...Some code here
 }

 public virtual IActionResult Login()
 {
  ...Some code here
 }
}


namespace ProjectB.Controllers
public partial class CustomerController : ProjectA.Controllers.CustomerController
{
 public override IActionResult Login()
 {
  ...Some code here
 }
}

When I try to override as above it gives me below error 当我尝试如上所述覆盖时,它给了我下面的错误

AmbiguousActionException: Multiple actions matched. AmbiguousActionException:多个动作匹配。 The following actions matched route data and had all constraints satisfied: ProjectA.Controllers.CustomerController.Index (ProjectA) ProjectB.Controllers.CustomerController.Index (ProjectB) 以下操作匹配路由数据并满足所有约束:ProjectA.Controllers.CustomerController.Index(ProjectA)ProjectB.Controllers.CustomerController.Index(ProjectB)

Though there is no Index action in ProjectB. 尽管在ProjectB中没有Index操作。 When I change the name of the conroller of ProjectB as: 当我将ProjectB的控制台名称更改为:

namespace ProjectB.Controllers
public partial class CustomCustomerController : ProjectA.Controllers.CustomerController
{
 public override IActionResult Login()
 {
  ...Some code here
 }
}

AmbiguousActionException is gone but still the override doesnt work. AmbiguousActionException消失了,但是覆盖仍然不起作用。 I dont get call in overriden action. 我没有在覆盖操作中接到电话。 Is it like .Net core doesnt support overriding any more because it works like charm without .net core with the same name of controllers. 难道.Net核心不再支持重写,因为没有.net核心且控制器名称相同时,它的工作原理就像魅力。 Please help.. 请帮忙..

As you have both class name as “CustomerController”. 因为两个类的名称都为“ CustomerController”。 And both may have using default routing. 而且两者都可能使用默认路由。 So for both controllers url should be same as /{controller}/{action} . 因此,对于两个控制器,url都应与/{controller}/{action}

So for same url there will be 2 actions available for each controllers. 因此,对于相同的URL,每个控制器将有2个操作可用。 That is why you are getting 这就是为什么你得到

“AmbiguousActionException” exception. “ AmbiguousActionException”异常。

When you change your child controller to “CustomCustomerController” then url will be changed for both controllers so no such ambiguity happen so it worked. 当您将子控制器更改为“ CustomCustomerController”时,两个控制器的url都将更改,因此不会发生此类歧义,因此可以正常工作。

As you said override not working. 正如你所说的覆盖不起作用。 In this case you have to use url as “/CustomCustomer/login” then it will go to Login action of CustomCustomerController. 在这种情况下,您必须使用url作为“ / CustomCustomer / login”,然后它将转到CustomCustomerController的Login操作。

EDIT 1 编辑1

You can update your code as below and it should work fine. 您可以按以下方式更新代码,它应该可以正常工作。 Create your controllers as below: 如下创建控制器:

namespace ProjectA.Controllers {
    public partial class CustomerController : Controller
    {
        public virtual IActionResult Index()
        {
            ...Some code here
        }

        public virtual IActionResult Login()
        {
            ...Some code here
        }
    }
}

namespace ProjectB.Controllers {
    public partial class CustomCustomerController : ProjectA.Controllers.CustomerController
    {
        public override IActionResult Login()
        {
            ...Some code here
        }
    }
}

And Update app.UseMvc(routes => part into startup.cs as below. Add customer route before default route. 并将app.UseMvc(routes => part更新到startup.cs ,如下所示。在default路由之前添加customer路由。

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "customer",
        template: "customer/{action}/{id?}",
        defaults: new { controller = "CustomCustomer", action = "Index" });

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

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

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