Multiple URLs same action method

这一生的挚爱 提交于 2019-12-13 08:23:49

问题


I need to share action methods between different controllers. Take for example the following 2 controllers:

public class AController : Controller
{
       public ActionResult Index()
       {
           //print AController - Index
       }

       public ActionResult Test()
       {
           //print test
       }
}

public class BController : Controller
{
     public ActionResult Index()
     {
         //print BController - Index
     }
}

Both controllers have an Index method which is different. The Test method however can be called from both controllers. So I want that when the following urls are entered the Test() method will execute:

  • AController/Test
  • BController/Test

I would appreciate any suggestions on how to achieve this.


回答1:


Assuming the implementation of the Test() action is the same for both controllers, refactor it into a common service:

public interface ITestService {
    string Test();
}

public TestService: ITestService {
    public string Test() {
        // common implementation
        return "The test result";
    }
}

Then set up Dependency Injection to acquire this service.

Your controllers then can use the common service.

public class AController : Controller {

    private readonly ITestService _testService;

    public AController(ITestService testservice) {
        _testService = testservice;
    }

    public ActionResult Test() {
        var vm = new TestViewModel();
        vm.TestResult = _testService.Test();
        return View("Test", vm);
    }
}

public class BController : Controller {

    private readonly ITestService _testService;

    public BController(ITestService testservice) {
        _testService = testservice;
    }

    public ActionResult Test() {
        var vm = new TestViewModel();
        vm.TestResult = _testService.Test();
        return View("Test", vm);
    }
}

Because the View Test.cshtml is rendered by both controllers, it should be placed in the Views\Shared\ folder.




回答2:


You can define your own routes as described here: https://docs.microsoft.com/aspnet/core/mvc/controllers/routing

So you can define as many routes as you like to point to the "Test" method inside "AController" just like this:

routes.MapRoute("Atest", "AController/Test",
        defaults: new { controller = "AController", action = "Test" });
routes.MapRoute("Btest", "BController/Test",
        defaults: new { controller = "AController", action = "Test" });

But you have to define them before the "default" route because otherwise the entered URL will match the default route conditions and so it will enter that route.

It´s also possible to define the route directly in top of the method.

public class AController : Controller
{
    [Route("/Some/Route")]
    public ActionResult Test() 
    {
    }
}



回答3:


I want to throw in an alternative solution. Create a base controller class to be inherited by the other two. Whatever you have there will be part of the children.

public class BaseController : Controller
{
    public ActionResult Index()
    {
        //print AController - Index
    }

    // Add more methods to be shared between the other controllers
}

public class AController : BaseController 
{
    // Has Index method already from parent

    // Unique method for A
    public ActionResult Test()
    {
        //print test 1
    }
}

public class BController : BaseController
{
    // Has Index method already from parent

    // Unique method for B
    public ActionResult Test()
    {
        //print test 2
    }
}

This implements the actual functionality in a single place. We use this method for many projects with no issues.



来源:https://stackoverflow.com/questions/49111585/multiple-urls-same-action-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!