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 ...
随机推荐
- Android RIL的java框架
Android RIL的Java部分也被分为了两个模块,RIL模块与Phone模块.其中RIL模块负责进行请求以及相应的处理,它将直接与RIL的原声代码进行通信.而Phone模块则向应用程序开发者提供 ...
- grafana的一些坑
坑1: 在设置alert的时候template中的变量是不被支持的,警告如下: 解决办法: 使用不带变量的具体sql查询 坑2: 时间轴的设置: 在更早的版本中时间轴的locale是无法设置的,就是说 ...
- C# 泛型详解---进阶编程(七)
今天我们来学习在C#的泛型技巧,传统的课本都在讲解什么是泛型,然后列举一大堆代码示例告诉你什么是泛型,今天我们就来聊聊更加本质的东西,我为什么要用泛型?它是来解决什么问题的?底层原理是什么? 简单来说 ...
- 【机器学习算法】bagging算法
参考 1.AdaBoost从原理到实现: 完
- ajax提交post请求出现数组被截断情况的解决方法
一.场景 php post 提交数据时传的数据时数组,没有多数据进行序列化处理.发现传到服务端时,部分数据丢失,查询了资料发现php对参数个数有限制,限制在php配置文件中(max_input_var ...
- vue 钩子
生命周期总结 这么多钩子函数,我们怎么用呢,我想大家可能有这样的疑问吧,我也有,哈哈哈. beforecreate : 举个栗子:可以在这加个loading事件 created :在这结束loadin ...
- 【问题】C4D中设置了界面颜色,如何恢复默认?
由于C4D没有恢复默认设置的选项,恢复默认的时候比较麻烦,这里简单删除一下配置文件就好了. 1.打开C4D设置,点击下面的[打开配置文件夹],并关掉C4D. (即C:\Users\你的用户名\AppD ...
- YUI JS压缩Ant脚本
<?xml version="1.0" encoding="UTF-8"?><!-- 对指定目录下的所有js进行压缩,放入指定位置 --> ...
- ATOM & Sublime Text 下MarkDown插件功能比较
ATOM & Sublime Text 下MarkDown插件功能比较 作者:net66 更新日期:2016-6-14 10:50 [一] 编辑器 Sublime Text3 vs Atom ...
- python 获取中文文件名的输出
#coding:utf8 if __name__ == '__main__': srcfile = u"D:/测试路径/测试文件.txt" f = open(srcfile.enc ...