Working forum post
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
@@ -42,8 +43,11 @@ namespace WebApi
|
||||
defaults: new { id = RouteParameter.Optional }
|
||||
);
|
||||
|
||||
// Dont include properties with null values
|
||||
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
|
||||
|
||||
// Force always json response
|
||||
var jsonFormatter = new JsonMediaTypeFormatter();
|
||||
//var jsonFormatter = new JsonMediaTypeFormatter();
|
||||
//optional: set serializer settings here
|
||||
//config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
|
||||
}
|
||||
|
||||
@@ -29,8 +29,19 @@ namespace WebApi.Controllers
|
||||
}
|
||||
|
||||
// POST api/<controller>
|
||||
public void Post([FromBody]string value)
|
||||
public IHttpActionResult Post([FromBody]FoorumDTO foorum)
|
||||
{
|
||||
if (foorum == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var ret = _foorumService.Add(foorum);
|
||||
if (ret != null)
|
||||
{
|
||||
return CreatedAtRoute("DefaultApi", new {controller = "Foorum", id = ret.FoorumId}, ret);
|
||||
}
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
// PUT api/<controller>/5
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
<h1>api</h1>
|
||||
<a href="/api/foorum">foorum</a>
|
||||
<button onclick="getForums()">GetAll</button>
|
||||
<button onclick="getForums()">GetAllForums</button>
|
||||
<button onclick="addForum()">AddForum</button>
|
||||
<code><pre id="output"></pre></code>
|
||||
|
||||
<script>
|
||||
var out = document.getElementById("output");
|
||||
|
||||
function getForums() {
|
||||
var out = document.getElementById("output");
|
||||
fetch("/api/foorum").then(resp => {
|
||||
return resp.json();
|
||||
}).then(data => {
|
||||
@@ -13,4 +15,21 @@
|
||||
out.innerText = JSON.stringify(data, null, 2);
|
||||
});
|
||||
}
|
||||
|
||||
function addForum() {
|
||||
var dto = {
|
||||
Author: "Veebileht",
|
||||
Body: "Siia tuleb <20>ge pikk sisu mingist asjast",
|
||||
Description: "Siia kirjeldus",
|
||||
Title: "Uhiuus postitus"
|
||||
}
|
||||
fetch("/api/foorum",
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user