我想从我的HTML客户端调用api。它给了我内部的服务器错误,但是当我用邮递员尝试它的时候它就起作用了。Web api内部服务器错误
这里是我的API代码
[AcceptVerbs("POST")]
public dynamic Add(string post,string title, string user)
{
if (post == null)
throw new Exception("Post content not added");
if (title == null)
throw new Exception("Post title not added");
var u = UserManager.FindByEmailAsync(user);
Blog blog = Blog.Create(u.Result.Account.RowKey, post, title).Save();
return new
{
id = blog.Id
};
}
我的HTML的客户是这样
var d = {
post: post,
title: title,
user: user
}
$.ajax({
type: 'POST',
url: apiUrl + 'Blog/Add',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(d)
}).done(function (data) {
console.log(data);
}).fail(function (error) {
});
,这里是我的路线API配置
config.Routes.MapHttpRoute(
name: "RPCApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new
{
id = RouteParameter.Optional
},
constraints: new
{
subdomain = new SubdomainRouteConstraint("api")
}
);
谁能帮助代码我在这里,并解释我为什么它与邮递员,而不是我的HTML客户端?
您收到了什么错误? – Yoav
未找到。 404.基本上无法达到api的终点。 – mohsinali1317
不要抛出'Exception' - 这将导致500.你应该返回400结果在这些情况下。 –