【发布时间】:2015-01-22 08:00:26
【问题描述】:
我正在尝试构建一个简单的 WebApi 服务,该服务将实时返回帖子的 cmets。所以该服务只实现了 Get 方法,它是一个简单的返回 IEnumerable 字符串用于传递的 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)
{
}
}
我也制作了 WebApiConfig 类并指定路由如下:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
在我的 Global.asax.cs 文件中,我为该路由添加了参考:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
在简单的局部视图中,我尝试每 8 秒调用一次此服务,因此帖子的 cmets 可以自己出现,无需刷新页面,以检查是否有其他用户对帖子发表了评论。
@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;
}
}
我的服务没有被调用,至少没有以正确的方式调用,导致新的 cmets 仅在刷新页面后才会显示。我知道 WebApi,尤其是在这种情况下,应该很容易实现并且非常简单,但我对这项技术完全陌生,我不知道我错过了什么或错误地实现了什么。 一直在尝试找到一些合适的教程来帮助我解决这个问题,但还没有任何帮助。
我在某处读到我应该为 WebDAV 在 Web.config 文件中添加一行,但这对我也没有帮助。
<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>
是否有人看到我没有做的事情以及可能的错误在哪里?另外,有人知道一些好的 .NET WebApi 教程吗?
附:当我使用以下命令直接从浏览器调用 WebApi Get 方法时:.../api/CommentApi/id 路由服务被调用并返回 cmets 以获取传递的帖子 id,所以服务没问题,我没有在代码很好......
【问题讨论】:
-
您是否收到任何脚本错误?您是否尝试过在浏览器开发者工具中查看请求返回的内容?
-
@jgauffin 我什么都没有……这真的很奇怪……我先尝试使用浏览器控制台,然后再使用 Fiddler……当我设置断点时,它永远不会到达那部分代码...我想我没有做好一些映射...
-
该页面是通过 ajax 请求生成的吗?如果是这种情况,将永远不会调用 document.ready 回调。尝试从页面中删除它(直接执行脚本)。
-
你使用哪个 jQuery?你看过这个网站吗? link
-
@Marcin 我已经尝试过该示例中的版本,但仍然没有...我之前使用的是旧版本...
标签: c# jquery asp.net-mvc-5 asp.net-web-api