Home > Blockchain >  How do I make a scaffolded page the first page
How do I make a scaffolded page the first page

Time:04-08

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.

CodePudding user response:

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 }
            );
        }
    }

CodePudding user response:

.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?}");
    });
}
  • Related