asp .net web api

时间:2020-05-21
本文章向大家介绍asp .net web api,主要包括asp .net web api使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

controller:

 

using Domain.Service;
using Domain.Service.Models;
using H5_WeiXin.App_Start;
using Microsoft.Ajax.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using System.Web.Http.Results;
using WLS.Project.DB.DbContext;

namespace H5_WeiXin.Controllers
{
    [RoutePrefix("api/Brand")]
    public class BrandController : ApiController
    {
        /// <summary>
        /// 获取授权书
        /// </summary>
        /// <param name="authorizeCode">授权编码</param>
        /// <remarks>
        /// code:"状态码"    200:"成功"  404:"不存在"<br/>
        /// pictureUrl:"图片路径"<br/>
        /// authorizeType:"授权类别"  expire:过期 authorized:"已授权"  notfound:"未找到"<br/>
        /// msg:"消息"
        /// </remarks>
        /// <returns>授权信息</returns>
        [HttpGet]
        [Route("GetAuthorizePicture")]
        [ResponseType(typeof(RetuenEntity))]
        [WebApiAuthFilterAttribute]
        //[AllowAnonymous]
        public JsonResult<RetuenEntity> GetAuthorizePicture(string authorizeCode)
        {
            RetuenEntity model = new RetuenEntity();
            try
            {
                var result = BrandService.Instance().GetAuthorizePicture(authorizeCode);
                if (result != null)
                {
                    model.code = 200;
                    model.pictureUrl = result.PicturePath;
                    if (result.AuthorizeEndDate < DateTime.Now.Date)
                    {
                        model.authorizeType = AuthorizeType.expire.ToString();
                    }
                    else
                        model.authorizeType = AuthorizeType.authorized.ToString();
                    model.msg = "请求成功!";
                }
                else
                {
                    model.code = 404;
                    model.authorizeType = AuthorizeType.notFound.ToString();
                    model.msg = "资源不存在";
                }
            }
            catch (Exception ex)
            {
                model.code = 404;
                model.msg = ex.Message;
            }
            return Json<RetuenEntity>(model);
        }

        [Route("Demo/{id}")]
        [HttpGet]
        public JsonResult<string> Get(int id)
        {
            return Json<string>("11");
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }


}

 

$flag 上一页 下一页