我有一个支付宝服务网关是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提交表单即可

在asp.net web form项目中添加webapi接口的更多相关文章

  1. 在ASP.NET Web API项目中使用Hangfire实现后台任务处理

    当前项目中有这样一个需求:由前端用户的一个操作,需要触发到不同设备的消息推送.由于推送这个具体功能,我们采用了第三方的服务.而这个服务调用有时候可能会有延时,为此,我们希望将消息推送与用户前端操作实现 ...

  2. 添加asp.net mvc到现有的asp.net web form 应用程序

    前言 asp.net mvc的前一版本为asp.net web Form(Asp.net mvc之前称为asp.net),其第一个版本与2002年年初发布.asp.net web form 属于.ne ...

  3. ASP.NET Web API实践系列01,以ASP.NET Web Form方式寄宿

    创建一个空的ASP.NET Web Form项目. 右键项目,添加新项,创建Web API控制器类,TestController. 删除掉TestController默认的内容,编写如下: using ...

  4. ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题

    转载 http://www.cnblogs.com/hiteddy/archive/2012/03/29/Prevent_Resubmit_When_Refresh_Reload_In_ASP_NET ...

  5. web项目中添加logger日志

    在项目中添加log4j.xml文件 log4j.xml文件 <?xml version="1.0" encoding="UTF-8" ?><! ...

  6. ASP.Net Web Form<一> aspx文件编译及呈现

    对比复习下JSP 1.jsp的本质是Servlet ,会在第一次被访问时会被翻译成一个类文件,从此对这个页面的访问都是由这个类文件执行后进行输出. aspx 本质是IHttpHandler 2.jsp ...

  7. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(4)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  8. 在ASP.NET Web API 2中使用Owin基于Token令牌的身份验证

    基于令牌的身份验证 基于令牌的身份验证主要区别于以前常用的常用的基于cookie的身份验证,基于cookie的身份验证在B/S架构中使用比较多,但是在Web Api中因其特殊性,基于cookie的身份 ...

  9. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(1)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

随机推荐

  1. 一. Redis 常用命令

    键值相关命令 1. KETS 查询所有的key 127.0.0.1:6379> keys * 1) "tony"2) "hexu1"3) "he ...

  2. iOS动画-从UIView到Core Animation

    首先,介绍一下UIView相关的动画. UIView普通动画: [UIView beginAnimations: context:]; [UIView commitAnimations]; 动画属性设 ...

  3. .net core xss攻击防御

    XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安全漏洞,它允许恶意 ...

  4. Python基础知识:if语句

    1.模拟网站确保用户名是否重复的方式,无视大小写,用到函数lower() #检查用户名是否重复 current_users=['admin','alex','lebran','kaobi','Jame ...

  5. C++中如何按照map中的value来进行排序

    sort函数无法对map进行排序,网上的方法一般是通过将map转为vector后,再来使用sort进行排序. 如下, 比较函数 bool cmp(const pair<int,int> & ...

  6. Lua基础之MetaTable(6)

    Lua基础之MetaTable(6) 转载地址:http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ 关于MetaTable的补充:ht ...

  7. Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作

    # -*- coding: gb2312 -*- #用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作 #作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.co ...

  8. break和continue语句(初学者)

    1.break语句可以从循环体内跳出循环体,即提前结束循环,接着执行循环下面的语句. 一般形式:break: break不能用于循环语句和switch语句之外的任何其他语句中. 注意:(1)break ...

  9. Apha冲刺! Day2 - 砍柴

    Alpha冲刺! Day2 - 砍柴 今日已完成 晨瑶:没做和主项目相关的事情,除了教队友用gitkraken. 昭锡:学习LitePal对数据库进行操作. 永盛:切换 API 管理平台,继续撰写 A ...

  10. asp.net core中使用HttpClient实现Post和Get的同步异步方法

     准备工作 1.visual studio 2015 update3开发环境 2.net core 1.0.1 及以上版本  目录 1.HttpGet方法 2.HttpPost方法 3.使用示例 4. ...