自建 webapi rest

    xiaoxiao2021-03-26  35

    环境准备

    1.Install-package  EntityFramework

    2.建立model模型:可以用ODA.net(DataFirst ,codeFirst),当然也可以LINQ to SQL类,也可以手动创建

    namespace pxtest.Models {     public partial class ProtData     {        [Key,Column(Order=1)]         public DateTime ActualDateTime { get; set; }         public string Identifier { get; set; }       [Key, Column(Order = 2)]         public string ActualTrackingPoint { get; set; }         public string PLC { get; set; }         public string SkidNo { get; set; }         public string HoleCode { get; set; }         public string LotNumber { get; set; }         public string Color { get; set; }         public string Model { get; set; }         public string BodyPaintCounter { get; set; }         public string PrimerColor { get; set; }         public string TimeSec { get; set; }     } }

    3.建立Context:context可以自动创建,也可以手动创建。

     public class pxContext:DbContext     {         public pxContext() : base("name=DefaultConnection") { }         public virtual DbSet<TrackingPointCode> TrackingPointCodes { get; set; }         public virtual DbSet<Production_plan> Production_plans { get; set; }         public virtual DbSet<ProtData> ProtDatas { get; set; }         protected override void OnModelCreating(DbModelBuilder modelBuilder)         {            // base.OnModelCreating(modelBuilder);            modelBuilder.Entity<ProtData>().HasKey(t => new { t.ActualDateTime, t.ActualTrackingPoint });                          modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();         }     }

    modelBuilder.Entity<ProtData>().HasKey(t => new { t.ActualDateTime, t.ActualTrackingPoint }); 为某表设置复合主键

    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();使调用的表为单数形式

    4.建立apicontroller

    创建的方法可以用web api 空建,web api 操作(entity framework)的创建。

    4.1 在global.cs中

        public class WebApiApplication : System.Web.HttpApplication     {         protected void Application_Start()         {             AreaRegistration.RegisterAllAreas();             GlobalConfiguration.Configure(WebApiConfig.Register);             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);             RouteConfig.RegisterRoutes(RouteTable.Routes);             BundleConfig.RegisterBundles(BundleTable.Bundles);             GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();         }     }

     GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();支持Json;

    4.2 

    private pxContext db = new pxContext();         // GET api/ProtDatas         public IQueryable<ProtData> GetProtDatas()         {             return db.ProtDatas;         }         // GET api/ProtDatas         public IQueryable<ProtData> GetProtDatas(string pt)         {             return db.ProtDatas.Where(p=>p.ActualTrackingPoint==pt);         }         // GET api/ProtDatas/5                 public IQueryable<ProtData> GetProtDatas(string pt, int startUTC)         {             //1481459940000 1481459940000000                          DateTime dt = new DateTime(1970, 1, 1).AddHours(8).AddSeconds(startUTC);             DateTime curdt = DateTime.Now;             IQueryable<ProtData> protdatas= db.ProtDatas.Where(p=>p.ActualTrackingPoint==pt && p.ActualDateTime>=dt && p.ActualDateTime<=curdt);                         return protdatas;         }

    5.对api rest的管理

    加入swashbuckle-Swagger for WebApi

    加入Swagger UI for .Net

    项目属性中的生成xml被勾选

    加入后更改SwaggerConfig.cs中的设置

     c.IncludeXmlComments(GetXmlCommentsPath());

     private static string GetXmlCommentsPath()         {             return string.Format("{0}/bin/px.XML", System.AppDomain.CurrentDomain.BaseDirectory);             // throw new System.NotImplementedException();         }

    对SwaggerNet.cs中进行注示

    //[assembly: WebActivator.PreApplicationStartMethod(typeof(px.App_Start.SwaggerNet), "PreStart")] //[assembly: WebActivator.PostApplicationStartMethod(typeof(px.App_Start.SwaggerNet), "PostStart")]

    转载请注明原文地址: https://ju.6miu.com/read-662953.html

    最新回复(0)