There are two handlers, the first should accept and process the format reference

https://localhost:44306/Activity/MyActivity 

and the second with the parameter

 https://localhost:44306/Activity/MyActivity?PortfolioId=1 

But the trouble is that everything takes the first, the second takes only

 https://localhost:44306/Activity/MyActivity/{PortfolioID} 

Those. after the activation parameter is a slash

 [HttpGet] public IActionResult MyActivity() { List<Activity> MyAct = db.Activities.Where(x => x.UserId == int.Parse(User.FindFirstValue("Id"))).ToList(); return View(MyAct); } [HttpGet("{PortfolioId}")] public IActionResult MyActivity(int PortfolioID) { List<Activity> MyAct = db.PortfolioActivities.Include(x => x.Activity).Where(x => x.PortfolioId == PortfolioID).Select(x=>x.Activity).ToList(); //db.Activities.Where(x => x.UserId == int.Parse(User.FindFirstValue("Id"))).ToList(); return View(MyAct); } 

    1 answer 1

    Probably it does not seem strange to you that you call different things with the same word and if you offer every single action (Create, Add, Delete) to call one and the same word you will not understand humor.

    However, you called two different things (MyActivities and ActivitiesFromPortfolio) by the same word - MyActivity.

    And you are trying to solve not the original problem (different things were called with the same name), but its child problems (how to set up routing so that the same urls go to different actions).

    I strongly recommend solving the original problem, and not thinking about how best to hammer in nails with a microscope .

    Here is an option:

    Here's another option:

    These are simple options in which you don’t even have to think about how to technically implement such routing. And they are the right ones.

    PS "There are only two really difficult problems in IT: how to give good names to entities and (skipped)"

    • It seems my question was not about how to write MVC correctly. I specifically asked how to divide the activations by parameters - LORD
    • @LORD Yes, yes - I perfectly understood your question (and answered him exactly “no way” and “no need to do that”) and I understand that with this comment you insist on your right to hammer nails with a microscope. But did you understand my answer? When I wrote the answer, I remembered about [ActionName] and looked at whether it was possible to somehow get out of this situation before recommending not to go in this direction. From the impasse in which you yourself were driven by the same route, do not get out. And I do not insist on a jackdaw - you can wait for another answer (all of a sudden you can get out), and this put a minus. - AK ♦
    • the problem is that I do not need to do this in this project, I need to learn how to do it. - LORD
    • @LORD Well, you can't do that. You can distinguish a single-variable route from a two-way route. And you have zero variables and one variable - they merge too much, int has a default value (You really understand that you have the route "{controller}/{action}/{id}" and for it the route with ID = 0 and with ID = 1 is no different from a route with ID = 2 or ID = 3?) and you also made the names of the controller and the action the same. Either change the names of the actions (as I suggested), or add the second parameter (it doesn’t fit in according to the logic). - AK ♦
    • the route with "{controller} / {action} / {id}" by ID should not differ, but what about "{controller} / {action} / {id}" and "{controller} / {action} /" - LORD