Reputation: 185
I use ASP.NET Core 3.1 LTS and route setting like this
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Backend",
pattern: "{area:exists}/{controller=Home}/{action=Index}");
endpoints.MapRazorPages();
});
and my areas razor page have a tag helper
<a class="btn btn-primary btn-sm" asp-area="Backend" asp-controller="Article" asp-action="Create"> Create</a>
usually this link will be /backend/article/create
but result is /article/create?area=backend
why result is wrong ?
Upvotes: 0
Views: 199
Reputation: 25380
The registering order of routes matters.
app.UseEndpoints(endpoints => { // register this route before the default one endpoints.MapControllerRoute( name: "Backend", pattern: "{area:exists}/{controller=Home}/{action=Index}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapRazorPages(); });
Make sure the area
of Backend
exists. Otherwise, it will be treated as querystring. For example, if you have already had an Area
of Backend
(Need to use the standard Area Folder Structure + Namespace, and annotate the controller with [Area("Backend")]
, see official docs)
[Area("Backend")] // important
public class ArticleController : Controller
{
public IActionResult Create(){ ...}
then the following code
<a asp-area="Backend" asp-controller="Article" asp-action="Create"> Create</a>
<a asp-area="AnAreaThatDoesnotExist" asp-controller="Article" asp-action="Create"> Create</a>
will generate routes as below:
<a href="/Backend/Article/Create"> Create</a>
<a href="/Article/Create?area=AnAreaThatDoesnotExist"> Create</a>
Note we specify an parameter of area=AnAreaThatDoesnotExist
which doesn't exist at all and then the area=AnAreaThatDoesnotExist
will be used as query string.
Upvotes: 2