ASP.NET Core 3.1中的区域端点路由 [英] Area endpoint routing in asp.net core 3.1

查看:126
本文介绍了ASP.NET Core 3.1中的区域端点路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题,我只是出于好奇而问

this is a simple question and im just asking out of curiosity

像这样设置到区域的端点路由:

ive set up endpoint routing to area as such:

     app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                name: "areas",
                pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

,这里是区域主页的控制器:

and here the controller for the area home page:

[Area("mynewarea")]
[Route("mynewarea/[controller]/[action]")]
public class HomeController : Controller
{

    public IActionResult Index()
    {
        ViewBag.me = "hello world";
        return View();
    }
}

这是视图中的标签帮助程序代码

here is the tag helper code in the view

<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
<a class="nav-link text-dark" asp-area="mynewarea" asp-controller="Home" asp-action="Index">mynewarea</a>

这一切都很好,但并不像我期望的那样始终
因此,例如,如果我将其放在URL中

this all totally works great but not consistently as i would expect
so for example if i put this in the URL

https://localhost/

它按预期工作,我进入主页
但是如果我把它放在URL中

it works as expected and i get to the home page
but if i put this in the URL

https://localhost/mynewarea

我得到404

但是如果我把它放在URL中

however if i put this in the URL

https://localhost/mynewarea/Home/Index

我的页面如预期的那样

如此:
1.有谁知道为什么我必须在我的区域网址中明确显示
2.有没有一种方法可以使默认区域URL隐式工作,即作为适当的默认URL

so:
1. does anyone know why i have to be explicit in my area URLs
2. is there a way to make default area URLs work implicitly, ie as a proper default URL

推荐答案

正在运行aspnetcore v3.1.0
升级到当前的v3.1.1时,问题就消失了

was running aspnetcore v3.1.0
when upgraded to current v3.1.1 problem went away

这篇关于ASP.NET Core 3.1中的区域端点路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆