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 ...
随机推荐
- PyCharm Debug调试程序
步骤一:在代码区要进行调试的代码行左侧单击鼠标左键,添加调试断点符号 步骤二:右键单击源文件或代码区内部,弹出菜单,选择[Debug ***] 或在右上角工具栏区单击[debug]按钮,快捷键[shi ...
- SSH MVC
1.MVC(Model-View-Controller)设计模式: 首先让我们了解下MVC(Model-View-Controller)的概念: MVC全名是Model View Controller ...
- java.lang.IndexOutOfBoundsException: setSpan (35 ... 35) ends beyond length 28
/************************************************************************************* * java.lang.I ...
- linux平台程序高精度延时问题select-usleep等
前言 微秒级别的延时... 1.能用 #include <unistd.h> int usleep(useconds_t usec); 微秒级:1/10^-6 2 ...
- 使用struts框架后的404错误
访问jsp界面后出现404错误,我开始以为是因为struts没有配置好,在网上找了很多解决方法, 试了一遍,无效, 最后在参考书上看到“struts2推荐把所有的视图界面存放在WEB-INF目录下,这 ...
- Appium笔记(二) 丶Appium的安装
一.前言 Appium 中有个很重要的组件Appium-Server,它主要用来监听我们的移动设备(真机或模拟器),然将不同编程语言编写的 appium 测试脚本进行解析,然后,驱动移动设备来运行测试 ...
- 来自官方的一些dbt 最佳实践
限制对原始数据的依赖性 您的项目将取决于存储在数据库中的原始数据.我们建议制作所谓的“基本模型”,以最大限度地减少对原始数据表的依赖性.在此约定中,基本模型可以具有以下职责: 仅选择与当前分析相关的字 ...
- 一个简单的批量更新oracle 数据库中 最近的服务商名称的数据
有一个需求是这样的,我们需要更新数据库中的数据,数据时这样的 1.大约50万以上 2. 数据中有较多的重复数据 3. 需要将表中最近的代理商的名称赋值给行中的服务商名称 4. 代理商的名称可能有多个, ...
- coffeescript学习
test2.coffeestdin = process.openStdin()stdin.setEncoding 'utf8' stdin.on 'data', (input) -> n ...
- php curl伪造来源ip和refer实例代码
php curl伪造来源ip和来路refer实例代码1: //随机IP function Rand_IP(){ $ip2id= round(rand(600000, 2550000) / 10000) ...