ASP.NET Core 3 Getting Started notes, using ASP.NET Core MVC framework for building Web applications

A, ASP.NET Core MVC output Hello World, Friend!

1. The introduction of ASP.NET Core MVC

Modify application startup class (Startup.cs), the introduction of the module and configure a default route MVC

public  class the Startup 
    { 
        
        public  void ConfigureServices (IServiceCollection Services) 
        { 
            // introduced MVC module 
            services.AddMvc (); 
        } 

        
        public  void the Configure (App IApplicationBuilder, IHostingEnvironment the env) 
        { 
            IF (env.IsDevelopment ()) 
            { 
                app.UseDeveloperExceptionPage (); 
            } 

            // configure a default route 
            app.UseMvc (routes => { 
                routes.MapRoute ( 
                    name: " the default " ,
                    template: "{controller}/{action}",
                    defaults: new { controller = "Home", action = "Index" }
                    );
            });
        }
    }

2. Create Controller and Action

Add in the project folder: Controllers, and create a class in a folderHomeController,继承类Controller,添加方法Index,用于向页面返回Hello World,Friend!

Correspondence relationship, i.e. class Controller, a method for the Action class

ControllerName=Home

ActionName=Index

public class HomeController:Controller
    {
        public IActionResult Index()
        {
            return Content("Hello World,Friend!");
        }
    }

3. Run

After the successful start of the project, VS Code will help us open the default browser and visit:http://localhost:5001

The reason why the display HomeController Index (Action) returns content, because we previously defined default route may correspond Action from {controller} / {action} access path, and we define default values:
controller = "Home", action = "Index"

//配置默认路由
            app.UseMvc(routes=> {
                routes.MapRoute(
                    name:"Default",
                    template: "{controller}/{action}",
                    defaults: new { controller = "Home", action = "Index" }
                    );
            });

We can also http://localhost:5001/home/indexshow access

Second, based on the use ASP.NET Core MVC view

1. Create a return View of Action

In the HomeControllerAdd Action: Time

public IActionResult Time () 
{ 
    // the current time into the server in ViewBag 
    ViewBag.ServerTime = the DateTime.Now;
     return View ( " Time " ); 
}

2. Create a view file

HomeController view subfolders created in the project folder Views, and creates a corresponding folder: Home.


The reason for this folder is created, because when we return view, only the specified ViewName, without specifying the full path. ASP.NET Core MVC framework will in turn read the default view files in the following project directory:

/Views/{ControllerName}
/Views/Shared
/Pages/Shared

If you find a view file will be rendered views, if not found it will throw an exception.

Create a view file /Views/Home/Time.cshtml, and writes @ ViewBag.ServerTime, ViewBag equivalent data carrier to action pages

When the view rendering @ ViewBag.ServerTime will output the contents of the Action assignment

3. Start project

Press F5 to start the project after the success of the project started in the browser, type http: // localhost: 5001 / home / time and visit, you will see the following output:

Guess you like

Origin www.cnblogs.com/xiaoahui/p/11723778.html