1. Create a project. There are two ways: one is to create a common class library; the other is to create a regular mudule project through the template (http://christoctemplate.codeplex.com/) in vs2012. For ease of explanation, we use the second method.
2. Add the following six DLL references (the bin folder in the home directory of the dnn website)
Dotnetnuke. dll
Dotnetnuke. Web. dll
System. net. http. dll
System. net. http. Formatting. dll
System. Web. http. dll
System. Web
3. Create a folder to store webapi-related files. Here we name it "webapi" and add the routing class "routemapper. CS and test class "welcomecontroller. CS (the Controller adding rules must also follow the mvc4.0 webapi rules and end with the Controller )". Note: When added, both are class files, rather than (especially the second) webapi classes. For example:
Routemapper. CSCode:
using system; using dotnetnuke. web. API; using system. web. HTTP; namespace using C. modules. dnnmodule1.webapi { Public class routemapper: iserviceroutemapper { Public void registerroutes (imaproute maproutemanager) {maproutemanager. maphttproute ( "dnnmodule1" , "default" , "{controller}/{action}/{name}" , New {name = routeparameter. optional}, New [] { "Objective C. modules. dnnmodule1.webapi "}) ;}}
The format of maproutemanager. maphttproute is:
Dnnmodules1-> Module name
Default-> route name. Same as web API in MVC
"{Controller}/{action}/{name}"-> controller, behavior, parameter. Same as web API in MVC
New{Name = routeparameter. optional},-> the default value of name is only, which can be omitted. Same as web API in MVC
New[] {"Christoc. modules. dnnmodule1.webapi"}-> The namespace of the Controller
Welcomecontroller. CS code:
Using System; Using System. net; Using System. net. HTTP; Using System. Web. HTTP; Using Dotnetnuke. Web. API; Namespace Christoc. modules. dnnmodule1.webapi {/// <Summary> /// API Parameter Data Model /// </Summary> Public Class Apidatacontent { /// <Summary> /// Custom data type (in JSON format) /// </Summary> Public Object Data {Get; set ;} /// <Summary> /// User permission authorization code /// </Summary> Public String Authkey {Get; Set ;}} Public Class Welcomecontroller: dnnapicontroller {[allowanonymous] [httpget] Public Httpresponsemessage hellozhangps (){ Return Request. createresponse (httpstatuscode. OK, "Hello zhangps! " );} [Allowanonymous] [httpget] Public Httpresponsemessage helloworld ( String Name ){ Return Request. createresponse (httpstatuscode. OK, "Hello world! " + Name);} [allowanonymous] [httppost] Public Httpresponsemessage postworld (apidatacontent content ){ Return Request. createresponse (httpstatuscode. OK, "Post World! " + Content. authkey );}}}
Apidatacontent is a complex parameter.
4. web API call method:/topics topmodules/dnnmodule1/API/welcome/postworld
5. Example: