接上一篇,项目本身是个Asp.Net MVC项目,之前用的是WCF实现的,这里研究一下在网站中的实现。
资料使用WebAPI实现的,参考:https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
-------------------------------------------------------------------------------------------------------------------------------------
找到一本书《ASP.NET Web API设计》,看了一下,发现里面说WCF是WebApi的前身,Asp.net Web Api是 Asp.net 和 WCF的结合。
根据第三章开始的教程操作,逐步理解WebAPI是什么。
模板创建的项目代码中最开始重要的是WebApiConfig和ValuesController
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
//[Authorize]
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
将头上的[Authorize]注释掉,不然运行后测试api时会提示没有经过权限验证。
运行项目,使用uri http://localhost:51290/api/values 测试,在浏览器中查看到xml格式的返回结果
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>value1</string>
<string>value2</string>
</ArrayOfstring>
用前一篇客户端测试,结果是json数组
["value1","value2"]
有意思,不同的客户端返回不同的内容吗
正好看到内容协商部分,
添加 request.Accept = "application/xml";
返回的结果就xml格式的了。
-------------------------------------------------------------------------------------------------------------------------------------------
根据教程写了个GreetingController
public class GreetingController : ApiController
{
public string GetGreeting()
{
return "Hello World! 1";
}
public static List<Greeting> _greetings = new List<Greeting>();
public HttpResponseMessage PostGreeting(Greeting greeting)
{
_greetings.Add(greeting);
var greetingLocation = new Uri(this.Request.RequestUri, "greeting/" + greeting.Name);
var response = this.Request.CreateResponse(HttpStatusCode.Created);
response.Headers.Location = greetingLocation;
return response;
}
public string GetGreeting(string id)
{
var greeting = _greetings.FirstOrDefault(g => g.Name == id);
return greeting.Message;
}
}
客户端验证代码
var client = new HttpClient();
var result = client.GetAsync("http://localhost:51290/api/greeting").Result;
var greeting = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(greeting);
没有意外的输出 Hello
修改为返回实体类,结果为json对象
public Greeting GetGreeting(string id)
{
return new Greeting() { Name = id, Message = "GetGreeting" };
}
{"Name":"1","Message":"GetGreeting"}
--------------------------------------------------------------------------------------------------------------------
项目上要对接的路径变量的问题:
尝试使用Route特性
[Route("{id}/message")]
public string GetGreetingMessage(string id)
{
return "GetGreetingMessage Id:"+id;
}
不行。
客户端代码:
var client = new HttpClient();
var result = client.GetAsync("http://localhost:51290/api/greeting/1/message").Result;
var greeting = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(greeting);
结果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IIS 10.0 Detailed Error - 404.0 - Not Found</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;}
code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;}
.config_source code{font-size:.8em;color:#000000;}
pre{margin:0;font-size:1.4em;word-wrap:break-word;}
ul,ol{margin:10px 0 10px 5px;}
ul.first,ol.first{margin-top:5px;}
fieldset{padding:0 15px 10px 15px;word-break:break-all;}
.summary-container fieldset{padding-bottom:5px;margin-top:4px;}
legend.no-expand-all{padding:2px 15px 4px 10px;margin:0 0 0 -12px;}
legend{color:#333333;;margin:4px 0 8px -12px;_margin-top:0px;
font-weight:bold;font-size:1em;}
a:link,a:visited{color:#007EFF;font-weight:bold;}
a:hover{text-decoration:none;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.4em;margin:10px 0 0 0;color:#CC0000;}
h4{font-size:1.2em;margin:10px 0 5px 0;
}#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS",Verdana,sans-serif;
color:#FFF;background-color:#5C87B2;
}#content{margin:0 0 0 2%;position:relative;}
.summary-container,.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
.content-container p{margin:0 0 10px 0;
}#details-left{width:35%;float:left;margin-right:2%;
}#details-right{width:63%;float:left;overflow:hidden;
}#server_version{width:96%;_height:1px;min-height:1px;margin:0 0 5px 0;padding:11px 2% 8px 2%;color:#FFFFFF;
background-color:#5A7FA5;border-bottom:1px solid #C1CFDD;border-top:1px solid #4A6C8E;font-weight:normal;
font-size:1em;color:#FFF;text-align:right;
}#server_version p{margin:5px 0;}
table{margin:4px 0 4px 0;width:100%;border:none;}
td,th{vertical-align:top;padding:3px 0;text-align:left;font-weight:normal;border:none;}
th{width:30%;text-align:right;padding-right:2%;font-weight:bold;}
thead th{background-color:#ebebeb;width:25%;
}#details-right th{width:20%;}
table tr.alt td,table tr.alt th{}
.highlight-code{color:#CC0000;font-weight:bold;font-style:italic;}
.clear{clear:both;}
.preferred{padding:0 5px 2px 5px;font-weight:normal;background:#006633;color:#FFF;font-size:.8em;}
-->
</style>
</head>
<body>
<div id="content">
<div class="content-container">
<h3>HTTP Error 404.0 - Not Found</h3>
<h4>您要找的资源已被删除、已更名或暂时不可用。</h4>
</div>
<div class="content-container">
<fieldset><h4>最可能的原因:</h4>
<ul> <li>指定的目录或文件在 Web 服务器上不存在。</li> <li>URL 拼写错误。</li> <li>某个自定义筛选器或模块(如 URLScan)限制了对该文件的访问。</li> </ul>
</fieldset>
</div>
<div class="content-container">
<fieldset><h4>可尝试的操作:</h4>
<ul> <li>在 Web 服务器上创建内容。</li> <li>检查浏览器 URL。</li> <li>检查失败请求跟踪日志并查看哪个模块正在调用 SetStatus。有关详细信息,请单击<a href="http://go.microsoft.com/fwlink/?LinkID=66439">此处</a>。</li> </ul>
</fieldset>
</div>
<div class="content-container">
<fieldset><h4>Detailed Error Information:</h4>
<div id="details-left">
<table border="0" cellpadding="0" cellspacing="0">
<tr class="alt"><th>Module</th><td> IIS Web Core</td></tr>
<tr><th>Notification</th><td> MapRequestHandler</td></tr>
<tr class="alt"><th>Handler</th><td> StaticFile</td></tr>
<tr><th>Error Code</th><td> 0x80070002</td></tr>
</table>
</div>
<div id="details-right">
<table border="0" cellpadding="0" cellspacing="0">
<tr class="alt"><th>Requested URL</th><td> http://localhost:51290/api/greeting/1/message</td></tr>
<tr><th>Physical Path</th><td> E:\Documents\Visual Studio 2015\Projects\WebApiTest\api\greeting\1\message</td></tr>
<tr class="alt"><th>Logon Method</th><td> 匿名</td></tr>
<tr><th>Logon User</th><td> 匿名</td></tr>
<tr class="alt"><th>请求跟踪目录</th><td> C:\Users\caiwanwei\Documents\IISExpress\TraceLogFiles\WEBAPITEST</td></tr>
</table>
<div class="clear"></div>
</div>
</fieldset>
</div>
<div class="content-container">
<fieldset><h4>More Information:</h4>
此错误表明文件或目录在服务器上不存在。请创建文件或目录并重新尝试请求。
<p><a href="http://go.microsoft.com/fwlink/?LinkID=62293&IIS70Error=404,0,0x80070002,16299">View more information »</a></p>
</fieldset>
</div>
</div>
</body>
</html>
参考资料:https://www.cnblogs.com/soundcode/p/4054001.html
他里面的{action}是放在id前面的,我考虑是否可以放到后面呢
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional }
);
}
}
[HttpGet]
[ActionName("message")]
public string GetGreetingMessage(string id)
{
return "GetGreetingMessage Id:"+id;
}
客户端结果:
"GetGreetingMessage Id:1"
可行。
Route里面明明有模板(uri模板)的怎么不行呢,还要继续学习。
------------------------------------------------------------------------------------------------------------------------------------------------
路由特性独立出去了