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!