C#的web项目ASP.NET

添加实体类和控制器类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class Company
    {
        public string companyCode { get; set; }
        public string companyName { get; set; }
        public string companyStatus { get; set; }
        public string jobGrade { get; set; }
        public string parentCompanyCode { get; set; }
        public int sort { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Services.Description;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class CompanyController : ApiController
    {
        Company[] companys = new Company[]
        {
            new Company { companyCode = "10", companyName = "XXX智能有限公司", companyStatus = "1", jobGrade = "测试",parentCompanyCode=null,sort=0 },
            new Company { companyCode = "20", companyName = "YYY科技有限公司", companyStatus = "1", jobGrade = "测试",parentCompanyCode=null,sort=1 }
        };
        public IEnumerable<Company> GetAllCompanys()
        {
            return companys;
        }
        public IHttpActionResult GetCompany(string id)
        {
            var company = companys.FirstOrDefault((p) => p.companyCode == id);
            if (company == null)
            {
                return NotFound();
            }
            return Ok(company);
        }
    }
}

在这里插入图片描述
在这里插入图片描述
那么,问题来了。访问路径我是从何得知的呢?其实是从WebApiConfig.cs文件中找到的。

WebApiConfig.cs



namespace WebApplication1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            //去掉xml返回格式、设置json字段命名采用
            var appXmlType =
                config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
        }
    }
}

其中,/{controller}/{action}/{id}的controller是CompanyController中的Company,action是方法名称GetAllCompanys,id是GetCompany方法的参数。

相关推荐

  1. web前端项目重构理解

    2024-06-07 20:10:02       48 阅读
  2. 一个C++版本web服务器

    2024-06-07 20:10:02       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 20:10:02       51 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 20:10:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 20:10:02       44 阅读
  4. Python语言-面向对象

    2024-06-07 20:10:02       55 阅读

热门阅读

  1. 数学部分学习

    2024-06-07 20:10:02       22 阅读
  2. 书籍数字字符串转换为字母组合的种数(4)0607

    2024-06-07 20:10:02       27 阅读
  3. Qt程序打包

    2024-06-07 20:10:02       26 阅读
  4. 【leetcode--统计优美子数组】

    2024-06-07 20:10:02       26 阅读
  5. 高级数据结构学习

    2024-06-07 20:10:02       18 阅读