我正在尝试创建一个web api来将数据发送到数据库并接收其他信息。我的web api将位于我不拥有的2个不同应用程序的中心!
但是当我尝试从控制台应用程序访问我的WebApi时,我总是得到
“(500)内部服务器错误。”
所以这是我的代码和我的课程(目前我正在一个假项目中练习以获得经验)
BTW我使用.Net 4(不是4.5)
一切都在一个大项目中,并用我将对每个班级说的解决方案分开
解决方案:DataModel
课程:权限
public class Permission
{
public int PermissionId { get; set; }
[MaxLength(50)]
public string Type { get; set; }
}
解决方案:WebService
类:PermissionController:ApiController
public class PermissionController : ApiController
{
[HttpGet]
public object Get([FromUri] int id)
{
Permission permission = PermissionDal.GetPermissionById(id);
if (permission == null)
{
return new { Success = Dal.Error };
}
return new { Success = Dal.Done, Permission = permission };
}
}
解决方案:DataAccess
类:PermissionDal
public static class PermissionDal
{
public static Permission GetPermissionById(int id)
{
return (from p in db.DbPermission where p.PermissionId == id select p).FirstOrDefault();
}
}
解决方案:控制台
课程:课程
static void Main(string[] args)
{
using (var client = new WebClient())
{
client.Headers.Clear();
client.Headers.Add("content-type", "application/json");
string result = client.DownloadString("http://localhost:50171/api/Permission?id=1");
Console.WriteLine(result);
}
}
我的webservice解决方案实际上是从项目WebService的visual studio属性在端口50171上运行。
我试过
string result = client.DownloadString("http://localhost:50171/api/Permission/1");
我试过
string result = client.DownloadString("http://localhost:50171/api/permission?id=1");
我试过
string result = client.DownloadString("http://localhost:50171/api/permission/1");
我总是得到同样的错误。