question

Ashokkumar-3624 avatar image
0 Votes"
Ashokkumar-3624 asked Ashokkumar-3624 commented

Dotnet core 2.1 where is the start up page routing ?

Hi,
When I created a dotnet core MVC project from default template given by visual studio 2017, I can not see anywhere Index page is hardcoded, When run the application its start serving Index.cshtml page.
Kindly help where is this settings configured.

Thanks,
Ashok

dotnet-aspnet-core-mvc
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MVC uses routing which maps a URL to an Action. An Action returns a View. Views are NOT access directly.

The default route is Home/Index and defined in the startup.cs file.

 routes.MapRoute(
     name: "default",
     template: "{controller=Home}/{action=Index}/{id?}");

I recommend reading the ASP.NET fundamentals documentation to make sure you understand ASP.NET Core basics.

Routing in ASP.NET Core 2.1
ASP.NET Core fundamentals



0 Votes 0 ·

Thank you for your reply, I agree, Asp.Net MVC I can see RouteConfig.cs.
But .NetCore 2.1 where is this , Is this default hard coded to Index route.

Thanks,
Ashok

0 Votes 0 ·

As explained in my first post, route configuration is in the startup.cs file. This information is mirrored in the links I provided above. Keep in mind, ASP.NET Core is very different from ASP.NET and reading the fundamentals docs will help a lot.

ASP.NET Core 2.1 long term support ends in August 2021. I recommend downloading Visual Studio 2019 community and learning .NET 5 which is the latest framework.


1 Vote 1 ·
Show more comments

1 Answer

ChaoDeng-MSFT avatar image
0 Votes"
ChaoDeng-MSFT answered

Hi @Ashokkumar-3624 ,

In version 2.1, the following code example is a MapRoute call example used by a typical ASP.NET Core MVC route definition:

 routes.MapRoute(
     name: "default",
     template: "{controller=Home}/{action=Index}/{id?}");

The preceding code is an example of a conventional routing. This style is called conventional routing because it establishes a convention for URL paths:

  • The first path segment maps to the controller name.

  • The second maps to the action name.

  • The third segment is used for an optional id. id maps to a model entity

Multiple routes
You can add multiple routes inside UseMvc by adding more calls to MapRoute. Doing so allows you to define multiple conventions, or to add conventional routes that are dedicated to a specific action, such as:

 app.UseMvc(routes =>
 {
    routes.MapRoute("blog", "blog/{*article}",
             defaults: new { controller = "Blog", action = "Article" });
    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
 });

For more examples, please refer to:
Routing in ASP.NET Core 2.1 and Routing to controller actions in ASP.NET Core




If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,

ChaoDeng


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.