Hi rani,
Refer below sample. To use routing you need to decorate the Controller and ActionMethod with Route attribute.
Controller
Home
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Route1()
{
return View();
}
}
Default
[Route("Default")]
public class DefaultController : Controller
{
public IActionResult Index()
{
return View();
}
[Route("one")]
public IActionResult Route1()
{
return View();
}
[Route("two")]
public IActionResult Route2()
{
return View();
}
[Route("two/{id?}")]
public IActionResult Route3(int id)
{
return View();
}
}
View
Home > Index
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h3>Home Controller Index</h3>
<form>
<a asp-controller="Default" asp-action="one">Route 1</a>
<hr />
<a asp-controller="Default" asp-action="two">Route 2</a>
<hr />
<a asp-controller="Default" asp-action="two" asp-route-id="1">Route 3</a>
</form>
</body>
</html>
Home > Route1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Route1</title>
</head>
<body>
<h3>Home Controller Route 1</h3>
</body>
</html>
Default > Index
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h3>Default Controller Index</h3>
</body>
</html>
Default > Route1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Route1</title>
</head>
<body>
<h3>Default Controller Route 1</h3>
</body>
</html>
Default > Route2
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Route2</title>
</head>
<body>
<h3>Default Controller Route 2</h3>
</body>
</html>
Default > Route3
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Route3</title>
</head>
<body>
<h3>Default Controller Route 3</h3>
</body>
</html>
Screenshot