Configure the use of MVC routing in the Core

Source: Internet
Author: User
This article mainly describes the use of the default MVC routing configuration in ASP., small series feel very good, and now share to everyone, but also for everyone to make a reference. Let's take a look at it with a little knitting.

The route this piece is not changed much in ASP. Only some usage tweaks, providing some more concise syntax.

There is certainly no problem with the support for custom routing, which should have been available from the MVC1.0 version of the feature.

Let's look at how the default MVC route is configured in ASP.

In general, the default route is sufficient when using MVC projects, which is the common way to get concrete methods through controller and action.

Starting with one of the most basic projects, perform the following steps to enable the project to support MVC routing

1. Create a blank ASP. NET Core (empty) Web project

2. Open Project.json and add the following dependencies under the "Dependencies" node

"MICROSOFT.ASPNETCORE.MVC": "1.0.0"

After saving, the project will automatically restore packages to local

3. Join the default MVC routing configuration

Open Startup.cs File

In the Configureservices method, add the following code

Services. Addmvc ();

This extension method injects some of the MVC services into the container.

In the Configure method, note the last backstop of the "Hello World" statement, which is responsible for whatever request it takes.

Then add the following code in the Configure method

App. Usemvcwithdefaultroute ();

This extension method actually uses a middleware, and the default URL template is consistent with the previous MVC version, which 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. To this configuration has been completed, but run the site directly display 404, does not show Hello World description has been effective, only empty project without controller, then define a controller bar.

Manually create the Controllers directory at the project root, then a new controller, named HomeController, and run the website directly (it should be refreshed).

The site continues to prompt for errors, but not 404, is prompted to find the index this view.

Continue to create the views directory in the project root directory, and then in the Views directory of a new home directory, in the home directory, create a new index.cshtml, fill in some content, refresh again.

Of course, this is the most basic configuration, such as to further support the implementation of cshtml inside the IntelliSense, support for static file routing, but also to add more dependencies and configuration.

"Recommended"

1. asp free Video Tutorial

2. asp Tutorials

3. Eon the ASP Basic video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.