Home > Backend Development > C#.Net Tutorial > Configuration for using MVC routing in Core

Configuration for using MVC routing in Core

Y2J
Release: 2017-05-05 13:33:55
Original
1854 people have browsed it

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();
Copy after login

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();
Copy after login

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"}
    );
   });
Copy after login

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();
  }
 }
Copy after login

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】

1. ASP Free Video Tutorial

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template