Last active
March 15, 2019 03:37
-
-
Save ainsofs/34bb5cccb05a0bad6712 to your computer and use it in GitHub Desktop.
Web Api Config - Can be used for DnnApiController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For returning objects | |
public class PagedResult<T> { | |
public List<T> Items { get; set; } | |
public long CurrentPage { get; set; } | |
public long ItemsPerPage { get; set; } | |
public long TotalItems { get; set; } | |
public long TotalPages { get; set; } | |
public string Text { get; set; } | |
public int ItemsDisplayed { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For DNN 7 and above only | |
using DotNetNuke.Web.Api; | |
public class RouteMapper : IServiceRouteMapper | |
{ | |
public void RegisterRoutes(IMapRoute mapRouteManager) | |
{ | |
//mapRouteManager.MapHttpRoute("[module name here]", "default", "{controller}/{action}", new[] {"[namespace of controller here]"}); | |
//eg | |
mapRouteManager.MapHttpRoute("MyServices", "default", "{controller}/{action}", new[] {"My.Services.Namespace"}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
namespace SampleService.Controllers { | |
public class SampleController : ApiController { | |
// GET api/<controller> | |
public HttpResponseMessage Get(){ | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} | |
// GET api/<controller>/5 | |
public HttpResponseMessage Get(int id) { | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} | |
// POST api/<controller> | |
public HttpResponseMessage Post([FromBody]string value) { | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} | |
// PUT api/<controller>/5 | |
public HttpResponseMessage Put(int id, [FromBody]string value) { | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} | |
// DELETE api/<controller>/5 | |
public HttpResponseMessage Delete(int id) { | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web; | |
using System.Web.Http; | |
using SampleService.App_Start; | |
namespace SampleService { | |
// Note: For instructions on enabling IIS6 or IIS7 classic mode, | |
// visit http://go.microsoft.com/?LinkId=9394801 | |
public class WebApiApplication : HttpApplication { | |
protected void Application_Start(){ | |
WebApiConfig.Register(GlobalConfiguration.Configuration); | |
} | |
} | |
} | |
++++++++++++ | |
using System.Web.Http; | |
namespace SampleService.App_Start { | |
public static class WebApiConfig { | |
public static void Register(HttpConfiguration config) { | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
} | |
} | |
} | |
++++++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//used in DNN 7 Services Framework | |
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using DotNetNuke.Services.Exceptions; | |
using DotNetNuke.Web.Api; | |
namespace SampleService.Controllers { | |
[AllowAnonymous] //for testing only - remove in production | |
public class MyDnnController : DnnApiController { | |
// GET api/<controller> | |
public HttpResponseMessage Get() { | |
try { | |
//add your code here | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} catch (Exception ex) { | |
Exceptions.LogException(ex); | |
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message); | |
} | |
} | |
// GET api/<controller>/5 | |
public HttpResponseMessage Get(int id) { | |
try { | |
//add your code here | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} catch (Exception ex) { | |
Exceptions.LogException(ex); | |
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message); | |
} | |
} | |
// POST api/<controller> | |
public HttpResponseMessage Post([FromBody]string value) { | |
try { | |
//add your code here | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} catch (Exception ex) { | |
Exceptions.LogException(ex); | |
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message); | |
} | |
} | |
// PUT api/<controller>/5 | |
public HttpResponseMessage Put(int id, [FromBody]string value) { | |
try { | |
//add your code here | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} catch (Exception ex) { | |
Exceptions.LogException(ex); | |
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message); | |
} | |
} | |
// DELETE api/<controller>/5 | |
public HttpResponseMessage Delete(int id) { | |
try { | |
//add your code here | |
return Request.CreateResponse(HttpStatusCode.NotImplemented, ""); | |
} catch (Exception ex) { | |
Exceptions.LogException(ex); | |
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment