You can use asp-area
rather than asp-route
.
Startup.cs
app.UseMvc(routes =>
{
routes.MapRoute(name: "out", template: "{area=Outbound}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Controller
[Area("Outbound")]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
<a href="/outbound/home/index">Go to Outoing’ Home Page by href="/outbound/home/index"</a> <br>
<a asp-area="Outbound" asp-controller="Home" asp-action="Index">Go to Outoing’ Home Page by asp-area</a><br>
@Html.ActionLink("Go to Outoing’ Home Page by @Html.ActionLink", "Index", "Home", new { area = "Outbound" })
Screenshots of test
asp-route
The asp-route
attribute is used for creating a URL linking directly to a named route. Using routing attributes, a route can be named as shown in the SpeakerController
and used in its Evaluations
action:
[Route("/Speaker/Evaluations",
Name = "speakerevals")]
public IActionResult Evaluations() => View();
In the following markup, the asp-route
attribute references the named route:
<a asp-route="speakerevals">Speaker Evaluations</a>
The Anchor Tag Helper generates a route directly to that controller action using the URL /Speaker/Evaluations
. The generated HTML:
<a href="/Speaker/Evaluations">Speaker Evaluations</a>
If asp-controller
or asp-action
is specified in addition to asp-route
, the route generated may not be what you expect.
To avoid a route conflict, asp-route
shouldn't be used with the asp-controller
and asp-action
attributes.
BTW, default route
and area route
automatically assign an order value to their endpoints based on the order they are invoked.
Difference between asp-route
and asp-controller
& asp-action
Route names(asp-route
) can be used to generate a URL based on a specific route.
In the same time, {controller}/{action}
also generate URL maps to Home
controller and Test
action.
//works
<a asp-controller="home" asp-action="test">asp-controller="home" asp-action="test"</a><br>
//works
<a asp-route="test">asp-route="test"</a><br>
//InvalidOperationException
<a asp-route="test" asp-controller="home" asp-action="test">asp-route="test" asp-controller="home" asp-action="test"</a>
InvalidOperationException
occurs when set both in one <a>
tag.