.NET CORE 5设置默认启动路由
我是一所旧学校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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
[oute]
默认控制器(客户端)中的属性。如果您定义了[ROUTE(“客户端”)]
,则仅使用域/客户端访问。解决方案1:将路由属性删除为
客户端,客户端/索引,“”
自动处理(program.cs中的mapRoute)解决方案2多路线:(在这种情况下,无需更改默认路由)
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)solution 2 multiple route: (in this case there is no need to change the default route)