Configuration for using MVC routing in Core
This article mainly introduces the configuration of using the default MVC routing in ASP.NET Core. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
The Route in ASP.NET Core has not changed much, but some usage has been adjusted and some more concise syntax has been provided.
Of course there is no problem with support for custom routing. This function should have been available since the MVC1.0 version.
Let’s first look at the configuration method of implementing default MVC routing in ASP.NET Core
Normally, when using MVC projects, the default routing is enough, which is the common one through Controller andActionHow to get the specific method.
Start from a basic project and perform the following steps to make the project support MVC routing
1. Create a blank ASP.NET Core (Empty) Web project
2. Open project.json and add the following dependency project under the "dependencies" node
"Microsoft.AspNetCore.Mvc": "1.0.0"
After saving, the project will automatically restore packages locally
3. Add the default MVC routing configuration
Open the Startup.cs file
In the ConfigureServices method, add the following code
services.AddMvc();
This extension method injects some services of Mvc into the container
In the Configure method, CommentsThe last one "hello world" statement, the function of this statement is to be responsible for any request.
Then add the following code to the Configure method
app.UseMvcWithDefaultRoute();
This extension method actually uses a Middleware. The default Url template is the same as the previous MVC version. The above The code is equivalent to the following effect
app.UseMvc(routes => { routes.MapRoute( name: "Default", template: "{controller}/{action}/{id?}", defaults: new {controller = "Home", action = "Index"} ); });
The final Startup.cs code is as follows
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); app.UseMvcWithDefaultRoute(); } }
4. At this point, the configuration has been completed, but the running site directly displays 404 and does not display Hello world, indicating that it has taken effect. It’s just that the Empty project does not have a Controller, so define a Controller.
Manually create the Controllers directory in the project root directory, then create a new Controller named HomeController, and then run the website directly (you should just refresh it).
The site continues to prompt errors, but it is no longer 404, but it prompts that the Index View cannot be found.
Continue to create the Views directory in the project root directory, then create a new Home directory under the Views directory, create a new Index.cshtml in the Home directory, fill in some content, and refresh again. That's it.
Of course, this is the most basic configuration. For example, to further support intelligent sensing in cshtml, support static file routing, etc., more must be added dependencies and configuration.
【Related Recommendations】
2. ASP Tutorial
3. Li Yanhui ASP basic video tutorial
The above is the detailed content of Configuration for using MVC routing in Core. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection

With the rapid development of the Internet, the needs of web applications are becoming more and more diverse. As a web developer, proficiency in development frameworks can improve development efficiency and code readability. As a high-performance web application framework, the Yii framework's routing configuration is also a very important part. Routing is the process in a web application of matching URL requests to corresponding controllers and action methods. In the Yii framework, routing rules consist of three parts, namely controllers, operation methods, and parameters. through routing

SpringMVC framework decrypted: Why is it so popular, specific code examples are needed Introduction: In today's software development field, the SpringMVC framework has become a very popular choice among developers. It is a Web framework based on the MVC architecture pattern, providing a flexible, lightweight, and efficient development method. This article will delve into the charm of the SpringMVC framework and demonstrate its power through specific code examples. 1. Advantages of SpringMVC framework Flexible configuration method Spr

The MVC (Model-View-Controller) pattern is a commonly used software design pattern that can help developers better organize and manage code. The MVC pattern divides the application into three parts: Model, View and Controller, each part has its own role and responsibilities. In this article, we will discuss how to implement the MVC pattern using PHP. Model A model represents an application's data and data processing. usually,

In Web development, MVC (Model-View-Controller) is a commonly used architectural pattern for processing and managing an application's data, user interface, and control logic. As a popular web development language, PHP can also use the MVC architecture to design and build web applications. This article will introduce how to use MVC architecture to design projects in PHP, and explain its advantages and precautions. What is MVCMVC is a software architecture pattern commonly used in web applications. MV

Developing MVC with PHP8 framework: Important concepts and techniques that beginners need to know Introduction: With the rapid development of the Internet, Web development plays an important role in today's software development industry. PHP is widely used for web development, and there are many mature frameworks that help developers build applications more efficiently. Among them, the MVC (Model-View-Controller) architecture is one of the most common and widely used patterns. This article will introduce how beginners can use the PHP8 framework to develop MVC applications.
