0

I am working on a project (Developed by using .Net Core), I have set some routes and one of them is not working e.g.

1) routes.MapRoute("HRDetail", "H-R/{TName}/{MId}", new { controller = "ABC", action = "XYZ1" });
2) routes.MapRoute("CL", "{SName}/{CName}/{CId}", new { controller = "ABC", action = "XYZ2" });

I have written the code in the same sequence in Startup class, and my action methods are as follows.

public async Task<IActionResult> XYZ2(string SName, string CName, Int16 CId)
{//for route#2}
public async Task<IActionResult> XYZ1( string TName, Int64 MId)
{//for route#1}

Now I want to hit on XYZ1 by using route#1 and the link (to hit on XYZ1 is being created dynamically) is like this http://localhost:4321/H-R/UK/1234. But the problem is that when i click on this link, it always take me to XYZ2 method.
I didn't set any route on controller or action method level.
Is there any solution plz?

2
  • I test it in asp.net core 2.2 without any problems.Could you create a new mvc project and test again?Could you show your complete startup.cs? Commented Mar 3, 2020 at 2:16
  • @XingZou first of all, thanks for your effort. I have been working on this project from 1 month, these routes were working fine, but suddenly it started to behave abnormally... So, to get rid of this problem, I just created a new controller for this purpose and its working fine with the same route... again thanks dear... Commented Mar 3, 2020 at 10:45

1 Answer 1

1

It seems, The route are getting confused. There are two ways you can fix this. 1) in your first route specify the regular expression which will say that first parameter will be a fixed string as H-R 2) in you second route specify the regular expression which will say that first parameter will never be H-R

1st

routes.MapRoute("HRDetail", "{ActionName}/{TName}/{MId}", new { controller = "ABC", action = "XYZ1" }, new{ActionName = "$your regularexpression to include only H-R$"});

OR

routes.MapRoute("CL", "{SName}/{CName}/{CId}", new { controller = "ABC", action = "XYZ2" }, new {SName = "$your regularexpression to exclude H-R$" });

PS: you need to put some efforts for regular expression

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.