首先创建一个asp.net core web应用程序

 第二步

目前官方预置了7种模板项目供我们选择。从中我们可以看出,既有我们熟悉的mvc、webapi,又新添加了razor page,以及结合比较流行的angular、react前端框架的模板项目。

空项目模板

program.cs

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore;
using microsoft.aspnetcore.hosting;
using microsoft.extensions.configuration;
using microsoft.extensions.logging;

namespace webapplication6
{
    public class program
    {
        public static void main(string[] args)
        {
            createwebhostbuilder(args).build().run();
        }

        public static iwebhostbuilder createwebhostbuilder(string[] args) =>
            webhost.createdefaultbuilder(args)
                .usestartup<startup>();
    }
}

实质就是控制台应用

startup.cs

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.aspnetcore.http;
using microsoft.aspnetcore.httpspolicy;
using microsoft.aspnetcore.mvc;
using microsoft.extensions.configuration;
using microsoft.extensions.dependencyinjection;

namespace webapplication6
{
    public class startup
    {
        public startup(iconfiguration configuration)
        {
            configuration = configuration;
        }

        public iconfiguration configuration { get; }

        // this method gets called by the runtime. use this method to add services to the container.
        public void configureservices(iservicecollection services)
        {
            services.configure<cookiepolicyoptions>(options =>
            {
                // this lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.checkconsentneeded = context => true;
                options.minimumsamesitepolicy = samesitemode.none;
            });


            services.addmvc().setcompatibilityversion(compatibilityversion.version_2_1);
        }

        // this method gets called by the runtime. use this method to configure the http request pipeline.
        public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }
            else
            {
                app.useexceptionhandler("/error");
                app.usehsts();
            }

            app.usehttpsredirection();
            app.usestaticfiles();
            app.usecookiepolicy();

            app.usemvc();
        }
    }
}

提供最原始的httpcontext

web api

创建完整的http服务,使用controller处理请求,仅返回数据无视图返回

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.mvc;

namespace webapplication7.controllers
{
    [route("api/[controller]")]
    [apicontroller]
    public class valuescontroller : controllerbase
    {
        // get api/values
        [httpget]
        public actionresult<ienumerable<string>> get()
        {
            return new string[] { "value1", "value2" };
        }

        // get api/values/5
        [httpget("{id}")]
        public actionresult<string> get(int id)
        {
            return "value";
        }

        // post api/values
        [httppost]
        public void post([frombody] string value)
        {
        }

        // put api/values/5
        [httpput("{id}")]
        public void put(int id, [frombody] string value)
        {
        }

        // delete api/values/5
        [httpdelete("{id}")]
        public void delete(int id)
        {
        }
    }
} 

startup.cs也加入了相关的mvc的服务和中间件

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.aspnetcore.http;
using microsoft.aspnetcore.httpspolicy;
using microsoft.aspnetcore.mvc;
using microsoft.extensions.configuration;
using microsoft.extensions.dependencyinjection;

namespace webapplication6
{
    public class startup
    {
        public startup(iconfiguration configuration)
        {
            configuration = configuration;
        }

        public iconfiguration configuration { get; }

        // this method gets called by the runtime. use this method to add services to the container.
        public void configureservices(iservicecollection services)
        {
            services.configure<cookiepolicyoptions>(options =>
            {
                // this lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.checkconsentneeded = context => true;
                options.minimumsamesitepolicy = samesitemode.none;
            });


            services.addmvc().setcompatibilityversion(compatibilityversion.version_2_1);
        }

        // this method gets called by the runtime. use this method to configure the http request pipeline.
        public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }
            else
            {
                app.useexceptionhandler("/error");
                app.usehsts();
            }

            app.usehttpsredirection();
            app.usestaticfiles();
            app.usecookiepolicy();

            app.usemvc();
        }
    }
}

web 应用程序-razor page

在asp.net mvc中razor作为一个高级视图引擎存在,razor提供了一种新的标记语义,其大大减少了用户输入且具有表现力,主要的特点是使用@符号去书写标记。 而在asp.net core中razor不仅仅是一种视图引擎,其提供了一种新的页面设计方式——razor page。其主要的特点在于:

  • 基于文件路径的路由系统;
  • razor page类似于webform,一种mvvm的架构,支持双向绑定;
  • razor page符合单一职责原则,每个页面独立存在,视图和代码紧密组织在一起。

web 应用程序-mvc

using system;
using system.collections.generic;
using system.diagnostics;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.mvc;
using webapplication5.models;

namespace webapplication5.controllers
{
    public class homecontroller : controller
    {
        public iactionresult index()
        {
            return view();
        }

        public iactionresult about()
        {
            viewdata["message"] = "your application description page.";

            return view();
        }

        public iactionresult contact()
        {
            viewdata["message"] = "your contact page.";

            return view();
        }

        public iactionresult privacy()
        {
            return view();
        }

        [responsecache(duration = 0, location = responsecachelocation.none, nostore = true)]
        public iactionresult error()
        {
            return view(new errorviewmodel { requestid = activity.current?.id ?? httpcontext.traceidentifier });
        }
    }
}