Skip to content

在Orchard的Web Api-Web Api in Orchard

Web Api is available in Orchard. You can implement a web api to fit your needs in a custom module.

Web Api可在Orchard中找到。您可以在自定义模块中实现Web api以满足您的需求。

Creating Api Controllers

创建Api控制器

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:

在Orchard中创建Api Controller的过程与在标准.NET Web Api应用程序中创建Api Controller的过程非常相似。创建您的控制器类并使其继承自ApiController:

namespace MyCustomModule.Controllers.Api{

namespace MyCustomModule.Controllers.Api {

    public class MyApiController : ApiController{

public class MyApiController:ApiController {

        public IHttpActionResult Get(){

<font color=#0099ff size=4 face="黑体">public IHttpActionResult Get(){</font>


            var itemsList = new List<string>{

                "Item 1",

                "Item 2",

                "Item 3"

            };



            return Ok(itemsList);

        }

<font color=#0099ff size=4 face="黑体">}</font>


    }

}

}

}

The above code sample will return the 3 item list shown in code when you hit the endpoint "/api/MyCustomModule/MyApi".

当您点击端点“/ api / MyCustomModule / MyApi”时,上面的代码示例将返回代码中显示的3项列表。

Declaring custom Api Routes

声明自定义Api路由

To generate more friendly Api routes, you follow a similar process to declaring custom MVC routes in Orchard. Implement the IHttpRouteProvider interface like so:

要生成更友好的Api路由,您可以按照类似的过程在Orchard中声明自定义MVC路由。像这样实现IHttpRouteProvider接口:

namespace MyCustomModule {

命名空间MyCustomModule {

    public class ApiRoutes : IHttpRouteProvider {

公共类ApiRoutes:IHttpRouteProvider {

        public IEnumerable<RouteDescriptor> GetRoutes() {

<font color=#0099ff size=4 face="黑体">public IEnumerable <RouteDescriptor> GetRoutes(){</font>


            return new RouteDescriptor[] {

                new HttpRouteDescriptor {

                    Name = "Default Api",

                    Priority = 0,

                    RouteTemplate = "api/myapi/{id}",

                    Defaults = new {

                        area = "MyCustomModule",

                        controller = "MyApi",

                        id = RouteParameter.Optional

                    }

                }

            };

        }

<font color=#0099ff size=4 face="黑体">}</font>

        public void GetRoutes(ICollection<RouteDescriptor> routes) {

<font color=#0099ff size=4 face="黑体">public void GetRoutes(ICollection <RouteDescriptor> routes){</font>


            foreach (RouteDescriptor routeDescriptor in GetRoutes()) {

                routes.Add(routeDescriptor);

            }

        }

<font color=#0099ff size=4 face="黑体">}</font>


    }

}

}

}

Now, the Api endpoint can be reached by hitting "/api/myapi".

现在,可以通过点击“/ api / myapi”来访问Api端点。

Configuring Web Api in Orchard

在Orchard中配置Web Api

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.

由于Orchard没有AppStart文件的概念,为了在Orchard中向Web Api添加自定义配置,您必须在Autofac模块中执行此操作。例如,以下内容将默认Web Api返回类型设置为Json,并确保Api返回的Json对象/属性遵循camelCased命名约定。

namespace MyCustomModule {

命名空间MyCustomModule {

    public class WebApiConfig : Module {

公共类WebApiConfig:Module {

        protected override void Load(ContainerBuilder builder) {

<font color=#0099ff size=4 face="黑体">protected override void Load(ContainerBuilder builder){</font>


            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();

            }

        }

<font color=#0099ff size=4 face="黑体">}</font>


    }

}

}

}

Conclusion

结论

This document should provide the basics of getting started with Web Api in Orchard. Enjoy!

本文档应提供Orchard中Web Api入门的基础知识。请享用!