在asp.net web form项目中添加webapi接口
2023-09-11 14:21:58 时间
在asp.net web form项目中添加webapi接口
我有一个支付宝服务网关是ASP.NET WEB FORM项目,但是最近这个网关需要对外提供几个接口,想了下,使用web api比较合适,实现很简单,GO
1,首先添加一个文件夹名字叫App_Start,貌似需要固定名称
2.在App_Start文件夹下添加WebApiConfig类,WebApiConfig类代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; namespace AlipayGateway.App_Start { public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }
3.在Global.asax文件的Application_Start函数中添加代码注册API路由规则
namespace AlipayGateway { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { GlobalConfiguration.Configure(WebApiConfig.Register); } } }
4.添加一个控制器
控制器代码如下:
using AliPayService; using Newtonsoft.Json; using System.Net.Http; using System.Text; using System.Web; using System.Web.Http; namespace AlipayGateway.Controllers { [RoutePrefix("api/sapi")] public class SapiController : ApiController { /// <summary> /// 发送模板消息 /// </summary> /// <returns></returns> [Route("sendtempmsg")] public HttpResponseMessage SendMsg() { string pay_type = HttpContext.Current.Request.Form["pay_type"]; string msg_content = HttpContext.Current.Request.Form["msg_content"]; string msg = MessageSendBiz.SendTemplateMsg(int.Parse(pay_type), msg_content); return GetHttpResponseMessage(msg); }private HttpResponseMessage GetHttpResponseMessage(string msg) { return new HttpResponseMessage { Content = new StringContent(msg, Encoding.GetEncoding("UTF-8"), "application/json") }; } } }
调用时向http://localhost:57841/api/sapi/sendtempmsg提交表单即可
相关文章
- .NET平台开源项目速览(8)Expression Evaluator表达式计算组件使用
- 【ASP.NET】ADO.NET
- ASP.NET Core使用Docker进行容器化托管和部署
- ASP.NET WebAPI构建API接口服务实战演练
- ASP.NET WebApi服务接口如何防止重复请求实现HTTP幂等性
- .NET开源Protobuf-net组件葵花手册
- 《ASP.NET MVC4 WEB编程》学习笔记------.net mvc实现原理ActionResult/View
- ASP.NET MVC 数据传递进阶 从数据库拿到数据后的三种方式
- ASP.NET中使用Entity Framework开发增删改查的Demo(EF增删改查+母版页的使用)
- [转] Asp.net 之 GC (垃圾回收机制)
- Asp.net Mvc (Filter及其执行顺序)
- ASP.NET Core 6框架揭秘实例演示[15]:针对控制台的日志输出
- [ASP.NET Core 3框架揭秘] Options[2]: 配置选项的正确使用方式[下篇]
- [ASP.NET Core 3框架揭秘] 依赖注入[9]:实现概述
- 如何在ASP.NET Core应用中实现与第三方IoC/DI框架的整合?
- ASP.NET Core的配置(4):多样性的配置来源[中篇]
- 【目录】开源Math.NET基础数学类库使用总目录
- ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
- ASP.NET Core微服务(六)——【redis命令详细列表3】
- ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
- asp.net core .net 3.1 HTTP Error 500.30 ANCM In-Process Start Failure