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的更多相关文章

  1. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  2. bootstrap + requireJS+ director+ knockout + web API = 一个时髦的单页程序

    也许单页程序(Single Page Application)并不是什么时髦的玩意,像Gmail在很早之前就已经在使用这种模式.通常的说法是它通过避免页面刷新大大提高了网站的响应性,像操作桌面应用程序 ...

  3. Hello Web API系列教程——Web API与国际化

    软件国际化是在软件设计和文档开发过程中,使得功能和代码设计能处理多种语言和文化习俗,在创建不同语言版本时,不需要重新设计源程序代码的软件工程方法.这在很多成熟的软件开发平台中非常常见.对于.net开发 ...

  4. ASP.NET Web API 跨域访问(CORS)

    一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...

  5. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  6. Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...

  7. angular2系列教程(八)In-memory web api、HTTP服务、依赖注入、Observable

    大家好,今天我们要讲是angular2的http功能模块,这个功能模块的代码不在angular2里面,需要我们另外引入: index.html <script src="lib/htt ...

  8. 我这么玩Web Api(二):数据验证,全局数据验证与单元测试

    目录 一.模型状态 - ModelState 二.数据注解 - Data Annotations 三.自定义数据注解 四.全局数据验证 五.单元测试   一.模型状态 - ModelState 我理解 ...

  9. 我这么玩Web Api(一):帮助页面或用户手册(Microsoft and Swashbuckle Help Page)

    前言 你需要为客户编写Api调用手册?你需要测试你的Api接口?你需要和前端进行接口对接?那么这篇文章应该可以帮到你.本文将介绍创建Web Api 帮助文档页面的两种方式,Microsoft Help ...

随机推荐

  1. UI基础:视图控制器.屏幕旋转.MVC 分类: iOS学习-UI 2015-07-02 22:21 62人阅读 评论(0) 收藏

    UIViewController 视图控制器,继承自UIResponder,作用:管理视图并且响应事件 功能: 1.分担APPdelegate的工作 2.实现模块独立,能提高复用性 创建UIViewC ...

  2. Java IO流经典练习题

    一.练习的题目 (一) 在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去:之后 ...

  3. ListBox item Sort

    将Rss内容读取到Listbox control中, 然后实现按照标题或发布日期进行排序. private void ListItemSort(string type) { if (type == & ...

  4. for (Sms sms : smsLists){}

    for (Sms sms : smsLists){ } //类似下面的for循环 :Smslists[i]!=NULL;i++) { Sms sms=Smslists[i]; } /*其实就是把Sms ...

  5. U盘安装电脑系统教程

    [怎么使用u盘安装系统.U盘装系统.如何用U盘安装系统.U盘制作系统.U盘引导.U盘启动.U盘量产.安装系统.如何设置U盘启动] 在电脑系统的日常使用中,经常会遇到系统崩溃或重新安装系统的情况,没有光 ...

  6. Almost Union-Find 并查集(脱离原来的树)

    h: 0px; "> I hope you know the beautiful Union-Find structure. In this problem, you’re to im ...

  7. LG3372 【【模板】线段树 1】

    介绍我的三种算法 1.线段树 既然这题是线段树板子,我还是尊重一下先发这个.跑的时间376ms不是很快,但也还是不错.O(nlogn). #include<iostream> #inclu ...

  8. MySQL--修改普通表为自增表

    =========================================================== 修改普通表为自增表 将普通表修改为自增表,可分为两种类型: 1.将现有列修改为自 ...

  9. Java使用Unsafe接口操作数组Demo

    public class unSafeArrayDemo { private static final sun.misc.Unsafe UNSAFE; private static final lon ...

  10. RESTful 知识点

    REST(英文:Representational State Transfer,简称REST) 对于资源的具体操作类型,由HTTP动词表示. 常用的HTTP动词有下面五个(括号里是对应的SQL命令). ...