I'm trying to build a simple WebApi service that will return comments for posts in real time. So the service only have Get method implemented and it's a simple returning IEnumerable of strings for passed Post ID:
public class CommentApiController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get(int id)
{
return new ApplicationDbContext().Posts.First(x => x.PostId == id).CommentId.Select(x => x.CommentText).ToList();
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
I've made WebApiConfig class too and specified routing as following:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
In my Global.asax.cs file I've added a reference for that routing:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
In simple partial view I'm trying to call this service every 8 seconds, so the comments for posts can appear by themselves, without need for refreshing the page, to check if some other users posted a comment on a post.
@model List<StudentBookProject.Models.Post>
<table class="table table-striped">
@foreach (var item in Model)
{
if (item.ImagePost != null)
{
<tr class="info">
<td>@item.CurrentDate</td>
</tr>
<tr class="info">
<td>
|@Html.ActionLink("Delete", "Delete", new { id = item.PostId }) |
@Html.ActionLink("Comment", "AddComment", new { id = item.PostId }, new { @class = "comment" }) |
</td>
</tr>
<tr class="info">
<td>
<img src="data:image/png;base64,@Convert.ToBase64String(item.ImagePost, 0, item.ImagePost.Length)" width="620" />
</td>
</tr>
<tr>
<td>
@Html.Partial("~/Views/Posts/ListComment.cshtml", item.CommentId)
</td>
</tr>
}
if (item.FilePost != null)
{
<tr class="info">
<td>@item.CurrentDate</td>
</tr>
<tr class="info">
<td>
| @Html.ActionLink("Delete", "Delete", new { id = item.PostId }) |
@Html.ActionLink("Comment", "AddComment", new { id = item.PostId }, new { @class = "comment" }) |
</td>
</tr>
<tr class="info">
<td>
File attachment
</td>
</tr>
}
if (item.TextPost != "")
{
<tr class="info">
<td>@item.CurrentDate</td>
</tr>
<tr class="info">
<td>
| @Html.ActionLink("Edit", "Edit", new { id = item.PostId }, new { @class = "lnkEdit" }) |
@Html.ActionLink("Delete", "Delete", new { id = item.PostId }) |
@Html.ActionLink("Comment", "AddComment", new { id = item.PostId }, new { @class = "comment" }) |
</td>
</tr>
<tr class="info">
<td>
@item.TextPost
</td>
</tr>
}
}
</table>
@{
int i = 0;
while (i < Model.Count())
{
if (Model[i].ImagePost != null || Model[i].TextPost != null || Model[i].FilePost != null)
{
<script>
$(document).ready(function () {
var showAllComments = function () {
$.ajax({
url: "/api/CommentApi/" + "@Model[i].PostId.ToString()"
}).done(function (data) {
var html = "";
for (item in data) {
html += '<div>' + data[item] + '</div>';
}
var divID = "@("quote" + Model[i].PostId.ToString())"
$('#' + divID).html(html);
setInterval(function () {
showAllComments();
}, 8000);
});
};
})
</script>
}
++i;
}
}
My service is not called, at least not in a proper way, cause new comments shows only after refreshing a page. I know that WebApi, especially in this case should be easy to implement and pretty straight forward, but I'm totally new to this technology and I don't have a clue what I've missed or implemented wrongly. Been trying to find some fitting tutorial to help me to solve this problem, but nothing helped me yet.
I've read somewhere that I've should add one line to Web.config file for WebDAV, but that didn't helped me also.
<handlers>
<remove name="WebDAV"/>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Does someone sees what I've didn't done and where is possible mistake? Also, does someone knows some good .NET WebApi tutorial?
P.S. When I made a call to a WebApi Get method directly from browser using: .../api/CommentApi/id route services gets invoked and returns comments for passed id of post, so service is ok, I'm not calling it in a code in a good way...