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. python3.6 ubuntu

    apt-get install python3.6-dev 安装 pip install mysqlclient 必备

  2. 如何控制jquery ui弹窗下方按钮水平居中

    1.问题背景 一般情况下,jquery ui弹窗下方的按钮是居右的,但是有时系统为了达到美观统一,需要将按钮放在中间 2.问题原因 <!DOCTYPE html> <html> ...

  3. 1.1.2A+B for Input-Output Practice (II)

    A+B for Input-Output Practice (II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768  ...

  4. proxool配置及测试(数据库用的MySQL)

    Proxool连接池设置   Proxool连接池是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是这个连接池提供监控的功能,方便易用,便于发现连接泄漏的情况. ...

  5. 【c++基础】accumulate

    accumulate // accumulate example #include <iostream> // std::cout #include <functional> ...

  6. opencv-python教程学习系列4-opencv绘图函数

    前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍opencv绘图函数,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: 系统 ...

  7. CF1083(div1)

    A. The Fair Nut and the Best Path 题意:给定有点权,有边权的树,让你选择一条链(也可以是只有一个点),使得点权之和-边权最大. 思路:裸的树形DP,我们用dp[i]表 ...

  8. Java8函数式编程学习笔记(初探)

    编程语言的整个目的就在于操作值,要是按照历史上编程语言的传统,这些值被成为一等值,而编程语言中的其他结构也许有助于表示值的结构,但在程序执行期间不能传递,因此为二等值,比如方法和类等则是二等值,类可以 ...

  9. touch事件(寻找触摸点 e.changedTouches)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  10. day 2Linux软件从主机安装到服务器和安装JDK软件

    软件安装 1.如何上传安装包到服务器**可以使用图形化工具,如: filezilla**可以使用sftp工具: alt+p 调出后,用put命令上传上传(如果不cd指定目录,则上传到当前用户的主目录) ...