网站WebSite如何添加WebAPI

一: 在网站下添加一个全局文件Global.asax,WebApplication1是WebApiConfig.cs的命名空间名称

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
        WebApplication1.WebApiConfig.Register(System.Web.Http.GlobalConfiguration.Configuration);
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
        // 或 SQLServer,则不引发该事件。

    }
       
</script>

二:添加引用文件,处理这些dll,还应该添加System.Web.Mvc.dll,System.Web.WebPages.dll,System.Web.WebPages.Razor.dll,System.Web.WebPages.Deployment.dll,System.Web.Razor.dll

三:在App_Code文件夹下添加路由配置WebApiConfig.cs和控制器ValuesController.cs(控制器名称自己名称,我命名的Values)

WebApiConfig.cs 代码如下:

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

namespace WebApplication1
{

    public static class WebApiConfig
    {

        public static void Register(HttpConfiguration config)
        {

            config.Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "api/{controller}/{action}/{id}",

                defaults: new { id = RouteParameter.Optional }

            );

        }

    }
}

ValuesController.cs代码如下:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

/// <summary>
/// ValuesController 的摘要说明
/// </summary>
public class ValuesController : ApiController
{
    public ValuesController()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //

    }
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
    #region POST方式传值 以json格式
    [HttpPost]
    public string GetMemberInfo(dynamic obj)
    {
       // JObject jo = (JObject)JsonConvert.DeserializeObject(data);
        List<Student> lstStu = new List<Student>();
        if (obj["ID"].ToString() == "1")
        {
            
            lstStu.Add(new Student() { StudentID = "001", StudentName = "张三" });
            lstStu.Add(new Student() { StudentID = "002", StudentName = "李四" });
            lstStu.Add(new Student() { StudentID = "003", StudentName = "王五" });
        }
        return JsonConvert.SerializeObject(lstStu);
    }
    #endregion 


    #region GET方式传值
    /// <summary>
    /// 获取学生信息
    /// </summary>
    /// <returns></returns>
    public string GetStudent()
    {
        List<Student> lstStu = new List<Student>();
        lstStu.Add(new Student() { StudentID = "001", StudentName = "张三" });
        lstStu.Add(new Student() { StudentID = "002", StudentName = "李四" });
        lstStu.Add(new Student() { StudentID = "003", StudentName = "王五" });
        return JsonConvert.SerializeObject(lstStu);
    }
    #endregion GET方式传值
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值