我如何使脚手架页面成为第一页

发布于 2025-01-19 13:40:22 字数 139 浏览 2 评论 0原文

因此,我正在关注 ASP.NET 的入门教程。到目前为止一切顺利,但是转到 /controllerName 相当乏味。有没有办法在项目启动时转到该索引页面?我在演练或任何文档中找不到它。我使用的是 6.0 版本。我对这个基本问题感到抱歉。

So I'm following the get started tutorial on ASP.NET . So far so good, however going to /controllerName is quite tedious. Is there a way how I can go to that index page on start up of the project? I couldn't find it in the walkthrough or any documentation. I'm using version 6.0 . I'm sorry for this basic question.

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

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

发布评论

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

评论(2

总以为 2025-01-26 13:40:22

.NET MVC 具有可以覆盖的内置默认值。

从根 / 路由的默认起始页是主控制器上的索引操作:

public class HomeController : 
{
    public IActionResult Index()
    {
        // ...
    }
}

坚持这些默认值是一个好主意,这样其他开发人员会发现更容易理解您的项目。

但是,如果您愿意,可以在 Startup.cs 配置方法中覆盖 .NET 5.0 中的默认值:

app.UseEndpoints(routes =>
{
    routes.MapControllerRoute(
        name: "default",
        pattern: "{controller=MyDefaultController}/{action=MyDefaultActionMethod}/{id?}");
    });
}

.NET MVC has built in defaults that can be overriden.

The default start page which will be routed from the root / is the Index action on the home controller:

public class HomeController : 
{
    public IActionResult Index()
    {
        // ...
    }
}

Sticking to these defaults is a good idea so other developers will find it easier to make sense of your project.

But you can override the defaults in .NET 5.0 if you wish in the Startup.cs Configure method:

app.UseEndpoints(routes =>
{
    routes.MapControllerRoute(
        name: "default",
        pattern: "{controller=MyDefaultController}/{action=MyDefaultActionMethod}/{id?}");
    });
}
烈酒灼喉 2025-01-26 13:40:22

您可以从路线配置更改所需的起始页面。它在
应用程序_启动>路由配置
路由配置

您可以根据需要更改控制器名称和索引页面。

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controllerName}/{actionName}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

You can change the desired starting page from Route config. It's in
App_Start>RouteConfig
route config

You can change it controller name and index page as you want.

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controllerName}/{actionName}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文