Call webapi in dnn 7.0 Module

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.