公司的很多项目前期一直是用的WebForms。但是因为业务的发展,公司要在原有的项目上接入移动端,webservice有点老旧了,现在比较流行RESTFul,于是乎就想到了WebAPI。
一、如果是新建项目最简单,文件=>新建=>项目=>Web=> ASP.NET Web 应用程序,在下方同时勾选Web Forms 和 Web API 核心引用即可,webfroms核心和WebAPI核心的应用程序就创建好了。
二、如果是原有的项目上增加WebAPI,只要将相关的包引用即可。
1.这里先创建WebForms 应用程序
2.Webfroms项目创建完成后,需要用到VS的NuGet包管理器。右击引用,选择 管理NuGet程序包。
选择 浏览,搜索WebAPI,选择第一个Microsoft.AspNet.WebApi;点击右边的安装后点击确定,后选择我接受,等到输出显示成功,则安装完成。
3.右击Web项目,添加名为App_Start的文件夹,在App_Start文件夹下创建名为WebApiConfig的cs文件。
清理命名空间,将类更改为static类型,添加必要代码,缺少引用的自行引用。
完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
namespace MyProject.App_Start
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
// Web API 路由
config.MapHttpAttributeRoutes();
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",//
defaults: new { id = RouteParameter.Optional,cont = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute(
// name: "DefaultApi2",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{cont}",
// defaults: new
// {
// controller = "ToCall",
// action = "ToCall",
// cont = RouteParameter.Optional
// }
//);
//https://localhost:44399/api/values/get?id=1//这是默认的
//https://localhost:44399/api/values/ToCall?cont=配置和服务
}
}
}
4.需要在Global.asax文件下的Application_Start方法中注册一下WebAPI,这里需要引用System.Web.Http;完整代码如下:
protected void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
GlobalConfiguration.Configure(WebApiConfig.Register);
}
5.接下来让我们来测试一下,新建一个Controller。名叫ValuesController
using DotNetSpeech;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Speech.Synthesis;
using System.Speech;
namespace MyProject
{
public class ValuesController : ApiController
{
public SpeechSynthesizer synth; //语音合成对象
//// GET api/<controller>
//public IEnumerable<string> Get()
//{
// return new string[] { "value1", "value2" };
//}
//// GET api/<controller>/5
//public string Get(string cont)
//{
// return cont;
//}
//// POST api/<controller>
//public void Post([FromBody] string value)
//{
//}
//// PUT api/<controller>/5
//public void Put(int id, [FromBody] string value)
//{
//}
//// DELETE api/<controller>/5
//public void Delete(int id)
//{
//}
//[HttpPost]
[HttpGet]
public string ToCall(string cont)
{
//调用示例:http://192.168.6.195:8081/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧
//https://localhost:44399/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧
//SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
//SpVoice sp = new SpVoice();
////sp.Voice = sp.GetVoices(" name=Microsoft Simplified Chinese ", "").Item(0);
//sp.Voice = sp.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,
//sp.Rate = 0;//语速
//sp.Volume = 100;//音量
//sp.Speak(cont, flags);
synth = new SpeechSynthesizer();
//使用 synth 设置朗读音量 [范围 0 ~ 100]
synth.Volume = 100;
//使用 synth 设置朗读频率 [范围 -10 ~ 10]
synth.Rate = 0;
synth.SelectVoice(synth.GetInstalledVoices()[0].VoiceInfo.Name);
//synth.SelectVoice("Microsoft Lili");
//Voice.Speak(ggg, SpFlags);
synth.SpeakAsync(cont);
return "12345";
}
[HttpGet]
public string GetAll()
{
return "Success";
}
}
}
6.浏览器访问http://localhost:27650/api/values/get?id=1,测试通过。