C# --VS2017新建WepApi(ASP.NET Web应用程序(.NET FrameWork))

1,新建ASP.NET Web应用程序

 

 2,选择“空”  核心引用“WebApi”打勾

 

 3,Web应用的启动文件是Global.asax.cs

 

4,WebApi我们主要使用Controllers和Models

和MVC的区别:没有View,只发送数据,不发送页面。

 5,在Controllers添加类

 

 

 5.1,在Controllers添加2个类

 

 另外一个类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using _002_新建WebApi.Models;

namespace _002_新建WebApi.Controllers
{
    //这个Controller返回Model数据
    public class ModelDataApiController:ApiController
    {
        public CustomModel GetCustomData()
        {
            var model = new CustomModel()
            {
                Id="ModelOne",
                Count = 1000
            };
            return model;
        }
    }
}

  

6,在Models中添加一个类

 

 6.1,添加引用

 

 7,这个时候启动项目,Get请求的方法会默认到达Controller层的以“Get”开头的方法中

 

 8,这时候我们发现出了能获取Controller的Get方法以外,访问其他方法都报错

解决办法:

8.1,在WebApiConfig中添加Action层

 

 8,2,方法要以Get开头或者指定为Get方法

 

9,现在可以访问方法了

 

 

posted @ 2020-10-02 00:12  包子789654  阅读(1402)  评论(0编辑  收藏  举报