Web Api in Orchard
Web Api in Orchard
Web Api is available in Orchard. You can implement a web api to fit your needs in a custom module.
Creating Api Controllers
The process of creating an Api Controller in Orchard is very similar to how you would do so in a standard .NET Web Api application. Create your controller class and have it inherit from ApiController:
namespace MyCustomModule.Controllers.Api{
public class MyApiController : ApiController{
public IHttpActionResult Get(){
var itemsList = new List<string>{
"Item 1",
"Item 2",
"Item 3"
}; return Ok(itemsList);
}
}
}
The above code sample will return the 3 item list shown in code when you hit the endpoint "/api/MyCustomModule/MyApi".
Declaring custom Api Routes
To generate more friendly Api routes, you follow a similar process to declaring custom MVC routes in Orchard. Implement the IHttpRouteProvider interface like so:
namespace MyCustomModule {
public class ApiRoutes : IHttpRouteProvider {
public IEnumerable<RouteDescriptor> GetRoutes() {
return new RouteDescriptor[] {
new HttpRouteDescriptor {
Name = "Default Api",
Priority = ,
RouteTemplate = "api/myapi/{id}",
Defaults = new {
area = "MyCustomModule",
controller = "MyApi",
id = RouteParameter.Optional
}
}
};
} public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (RouteDescriptor routeDescriptor in GetRoutes()) {
routes.Add(routeDescriptor);
}
}
}
}
Now, the Api endpoint can be reached by hitting "/api/myapi".
Configuring Web Api in Orchard
Since Orchard doesn't have the concept of an AppStart file, in order to add custom configuration to Web Api in Orchard, you must do so in an Autofac module. For example, the following will set the default Web Api return type to Json, and will ensure that Json objects/properties returned by the Api follow the camelCased naming convention.
namespace MyCustomModule {
public class WebApiConfig : Module {
protected override void Load(ContainerBuilder builder) {
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); var jsonFormatter = GlobalConfiguration.Configuration.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault(); if (jsonFormatter != null) {
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
}
}
Conclusion
This document should provide the basics of getting started with Web Api in Orchard. Enjoy!
Web Api in Orchard的更多相关文章
- 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用
由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...
- bootstrap + requireJS+ director+ knockout + web API = 一个时髦的单页程序
也许单页程序(Single Page Application)并不是什么时髦的玩意,像Gmail在很早之前就已经在使用这种模式.通常的说法是它通过避免页面刷新大大提高了网站的响应性,像操作桌面应用程序 ...
- Hello Web API系列教程——Web API与国际化
软件国际化是在软件设计和文档开发过程中,使得功能和代码设计能处理多种语言和文化习俗,在创建不同语言版本时,不需要重新设计源程序代码的软件工程方法.这在很多成熟的软件开发平台中非常常见.对于.net开发 ...
- ASP.NET Web API 跨域访问(CORS)
一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...
- Web Api 入门实战 (快速入门+工具使用+不依赖IIS)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...
- Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...
- angular2系列教程(八)In-memory web api、HTTP服务、依赖注入、Observable
大家好,今天我们要讲是angular2的http功能模块,这个功能模块的代码不在angular2里面,需要我们另外引入: index.html <script src="lib/htt ...
- 我这么玩Web Api(二):数据验证,全局数据验证与单元测试
目录 一.模型状态 - ModelState 二.数据注解 - Data Annotations 三.自定义数据注解 四.全局数据验证 五.单元测试 一.模型状态 - ModelState 我理解 ...
- 我这么玩Web Api(一):帮助页面或用户手册(Microsoft and Swashbuckle Help Page)
前言 你需要为客户编写Api调用手册?你需要测试你的Api接口?你需要和前端进行接口对接?那么这篇文章应该可以帮到你.本文将介绍创建Web Api 帮助文档页面的两种方式,Microsoft Help ...
随机推荐
- C++ 写的地图控件,支持google 百度 在线离线地图
C++处理google 百度地图在网上查阅了很多都是通过浏览器方式显示地图信息, 跟我目前项目很不符合, 所以仔细研究了一下C++方式显示地图.通过地图投影以及墨卡托投影,在通过平面地图计算经纬度. ...
- requests.exceptions.MissingSchema
requests.exceptions.MissingSchema: Invalid URL '//p9.pstatp.com/list/pgc-image/1538380201743a84869e0 ...
- stm32 内部温度传感器的配置
STM32的内部温度传感器和ADCx—IN16输入通道相连接.且温度传感器推荐采样时间为17.1us,支持的温度范围为:-40~125度,精度比较差,± 5℃左右. 计算温度值:T(℃) ={(V25 ...
- [LeetCode&Python] Problem 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- 和菜鸟一起学linux内核源码之基础准备篇
来源:http://blog.csdn.net/eastmoon502136/article/details/8711104 推荐阅读:linux内核源码最初版linux内核源代码,简单易懂,适合初学 ...
- 【HDU5421】Victor and String(回文树)
[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...
- WPF控件NumericUpDown (转)
WPF控件NumericUpDown示例 (转载请注明出处) 工具:Expression Blend 2 + Visual Studio 2008 语言:C# 框架:.Net Framework 3. ...
- 产品思维&技术思维&工程思维
产品思维 产品思维的起源是用户(或客户)价值.用户价值是通过技术手段以产品或服务的形态去解决用户的痛点,或带去爽点.毫无疑问,工程师在日常工作中应时刻关注并理清自己的工作与用户(或客户)价值的联系,并 ...
- 一 JAVA整体概念以及安装部署
JAVA 基本概念 JVM(JAVA virtual machine)java虚拟机,是java的能跨平台的核心,java的跨平台实现,就是在各种系统中布置JVM,然后java应用运行在JVM中,相 ...
- hadoop之 心跳时间与冗余快清除
1.Hadoop datanode节点超时时间设置 datanode进程死亡或者网络故障造成datanode无法与namenode通信,namenode不会立即把该节点判定为死亡,要经过一段时间,这段 ...