.NET CORE 5设置默认启动路由

发布于 2025-02-10 11:38:33 字数 2220 浏览 0 评论 0原文

我是一所旧学校ASP.NET& Angularjs Dev和我现在正在研究现有的.NET 5核心应用程序(将迁移到.NET Core 6)。这对我来说是新技术,我仍在弄清楚事情。我期望非常简单的一件事是更改Web UI的起点,但我无法按预期进行这项工作。 目前,该应用程序在默认控制器路线Home上启动。我想更改此操作,因此,我希望它不会降落在家里(通过myapp.mydomain.com),我希望它降落在另一个控制器上的客户(通过myapp.mydomain.com/clients)上。 我已经尝试在定义默认路由的启动.cs上进行更改:

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

clientscontroller:

namespace MyCompany.MyApp.Controllers
{
    [Authorize]
    public class ClientsController : Controller
    {
        private readonly ILogger<ClientsController> _logger;
        private readonly IClientService _service;
        private readonly IAppVersionService _appVersionService;
        private readonly IIisService _iisService;
        private readonly IWindowsServices _windowsServices;
        private readonly IAuthHelpers _authHelpers;

        public ClientsController(ILogger<ClientsController> logger, IClientService service, 
            IAppVersionService appVersionService, IIisService iisService, IWindowsServices windowsServices, IAuthHelpers authHelpers)
        {
            _logger = logger;
            _service = service;
            _appVersionService = appVersionService;
            _iisService = iisService;
            _windowsServices = windowsServices;
            _authHelpers = authHelpers;
        }

        [HttpGet]
        [Route("/Clients")]
        public async Task<IActionResult> Index(int? pageNumber)
        {
            _logger.LogInformation("Client Index starting...");
            await SyncAppVersionsWithS3();
            await AddVersionsToViewData();
            ViewBag.Error = TempData["Error"];
            ViewBag.Success = TempData["Success"];
            var paginatedClients = await PaginatedList<ClientDto>
                .CreateAsync(_service.Query(), pageNumber ?? 1, 10);
            return View(paginatedClients);
        }

但是,在启动时导致404。回到旧的ASP.NET天,我只是在ASPX文件上单击rt键,然后选择设置为启动。我只想在这里做同等的事情,而不会是kludge。我该怎么做?谢谢!

I'm an old school ASP.Net & AngularJS dev and I'm now working on an existing .Net 5 Core app (that will be migrated to .Net Core 6). This is new tech to me and I'm still figuring things out. One thing I expected to be very simple was to change the starting point of the web UI but I have not been able to make this work as expected.
Right now the app starts up on the default Controller Route, Home. I want to change this so instead of landing on Home (via myapp.mydomain.com) I want it to land on another controller, Clients (via myapp.mydomain.com/Clients).
I've tried making a change in Startup.cs where the default route is defined:

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

ClientsController:

namespace MyCompany.MyApp.Controllers
{
    [Authorize]
    public class ClientsController : Controller
    {
        private readonly ILogger<ClientsController> _logger;
        private readonly IClientService _service;
        private readonly IAppVersionService _appVersionService;
        private readonly IIisService _iisService;
        private readonly IWindowsServices _windowsServices;
        private readonly IAuthHelpers _authHelpers;

        public ClientsController(ILogger<ClientsController> logger, IClientService service, 
            IAppVersionService appVersionService, IIisService iisService, IWindowsServices windowsServices, IAuthHelpers authHelpers)
        {
            _logger = logger;
            _service = service;
            _appVersionService = appVersionService;
            _iisService = iisService;
            _windowsServices = windowsServices;
            _authHelpers = authHelpers;
        }

        [HttpGet]
        [Route("/Clients")]
        public async Task<IActionResult> Index(int? pageNumber)
        {
            _logger.LogInformation("Client Index starting...");
            await SyncAppVersionsWithS3();
            await AddVersionsToViewData();
            ViewBag.Error = TempData["Error"];
            ViewBag.Success = TempData["Success"];
            var paginatedClients = await PaginatedList<ClientDto>
                .CreateAsync(_service.Query(), pageNumber ?? 1, 10);
            return View(paginatedClients);
        }

But that results in a 404 at startup. Back in the old asp.net days I'd just rt-click on a aspx file and select Set As Startup. I just want to do the equivalent here without it being a kludge. How do I do that? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑看君怀她人 2025-02-17 11:38:33

问题是[oute]默认控制器(客户端)中的属性。如果您定义了[ROUTE(“客户端”)],则仅使用域/客户端访问。

解决方案1:将路由属性删除为客户端,客户端/索引,“”自动处理(program.cs中的mapRoute)

 [HttpGet]
 public async Task<IActionResult> Index(int? pageNumber)
 {
     ...
 }

解决方案2多路线:(在这种情况下,无需更改默认路由)

[HttpGet]
[Route("")]
[Route("Clients")]
[Route("Clients/Index/{pageNumber?}")]
public async Task<IActionResult> Index(int? pageNumber)
{
         ...
}

The problem is [Route] attribute in default controller (Clients). if you are defined [Route("Clients")], only access with domain/clients.

solution 1: Remove route attribute as Clients, Clients/Index, "" are handled automatically (MapRoute in program.cs)

 [HttpGet]
 public async Task<IActionResult> Index(int? pageNumber)
 {
     ...
 }

solution 2 multiple route: (in this case there is no need to change the default route)

[HttpGet]
[Route("")]
[Route("Clients")]
[Route("Clients/Index/{pageNumber?}")]
public async Task<IActionResult> Index(int? pageNumber)
{
         ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文