LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

.NET4.0如何配置WebApi

Ccoffee
2025年4月17日 15:54 本文热度 259
:.NET4.0如何配置WebApi

步骤 1:安装必要的 NuGet 包

  1. 右键项目 -> 管理 NuGet 程序包,安装4.0.3056版本

  2. 搜索并安装 Microsoft.AspNet.WebApi(核心包)

  3. 安装 Microsoft.AspNet.WebApi.WebHost(Web 宿主支持)

配置 Web API 路由

配置 Web API 路由,打开 Global.asax 文件,在 Application_Start 中添加 Web API 路由。

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

<%@ Import Namespace="System.Web.Http" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)

    {

        //在应用程序启动时运行的代码 第一个访问网站的用户会触发该方法

        //SqlKey myCheck_page = new SqlKey();  //启动黑名单验证

        //myCheck_page.response_time();

        // 旧版 Web API 1 的初始化方式

        var config = GlobalConfiguration.Configuration;

        // 注册默认路由

        config.Routes.MapHttpRoute(

            name: "DefaultApi",

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

            defaults: new { id = System.Web.Http.RouteParameter.Optional }

        );

        // 移除 XML 格式化器,强制使用 JSON

        config.Formatters.Remove(config.Formatters.XmlFormatter);

        // 设置 JSON 为默认格式(无需直接操作 MediaTypeHeaderValue)

        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

        // 关键:显式设置控制器激活器

        config.Services.Replace(

            typeof(System.Web.Http.Dispatcher.IHttpControllerActivator),

            new System.Web.Http.Dispatcher.DefaultHttpControllerActivator()

        );

    }

添加 Web API 控制器

创建一个简单的控制器 在App_Code文件加,添加TestController.cs

using System;

using System.Web.Http;

using System.Collections.Generic;

using Newtonsoft.Json;

using System.Text;


public class TestController : ApiController

{

    [HttpPost]

    public void GetFDSFDSFDS(LoginModel model)

    {

        var data = new { status = model.Name, Email = model.Email };

        var json = JsonConvert.SerializeObject(data);

        var response = System.Web.HttpContext.Current.Response;

        response.ContentType = "application/json";

        response.ContentEncoding = Encoding.GetEncoding("utf-8");

        response.Write(json);

        response.End();

    }


    [HttpGet]

    public void Get(string id)

    {

        var response = System.Web.HttpContext.Current.Response;

        response.ContentType = "application/json";

        response.ContentEncoding = Encoding.GetEncoding("utf-8");

        response.Write("id="+id);

        response.End();

    }

    

}


public class LoginModel

{

    public string Name { get; set; }

    public string Email { get; set; }

}

配置 Web.config(关键)

在 <system.webServer> 节点下添加以下配置,确保路由正确处理:

<system.webServer>

  <handlers>

    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />

    <add name="ExtensionlessUrlHandler-Integrated-4.0" 

         path="api/*" 

         verb="*" 

         type="System.Web.Handlers.TransferRequestHandler" 

         preCondition="integratedMode,runtimeVersionv4.0" />

  </handlers>

</system.webServer>

验证是否成功

访问以下 URL:

  • http://localhost:端口号/api/test → 应返回 "Hello from Web API 1!"

  • http://localhost:端口号/api/test/123 → 应返回 "You requested ID: 123"


该文章在 2025/4/17 16:10:05 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved