Hasnain Latif
Hasnain Latif

Reputation: 65

ASP.Net Core MVC Routing Issue

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?

Upvotes: 0

Views: 84

Answers (1)

Sachin Vishwakarma
Sachin Vishwakarma

Reputation: 874

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

Upvotes: 1

Related Questions