【发布时间】:2017-02-05 05:20:31
【问题描述】:
我正在使用此 URL 调用服务:http://localhost:7951/odata/Student 但我得到了错误
404 未找到
我哪里做错了?
我的 DbContext 类如下:
public class DataContext : DbContext
{
public DataContext()
: base("name=FrameworkConnStr")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<StudentAddress> StudentAddresses { get; set; }
}
[Table("M_DEMIR.STUDENT")]
public partial class Student
{
public Student()
{
StudentAddress = new HashSet<StudentAddress>();
}
[Column("ID"), Key]
public int Id { get; set; }
[Column("NAME", TypeName = "varchar2"), MaxLength(25)]
public string Name { get; set; }
[Column("SURNAME", TypeName = "varchar2"), MaxLength(50)]
public string Surname { get; set; }
public virtual ICollection<StudentAddress> StudentAddress { get; set; }
}
[Table("M_DEMIR.STUDENTADDRESS")]
public partial class StudentAddress
{
[Column("ID"), Key]
public int Id { get; set; }
[Column("STUDENT_ID")]
public int StudentId { get; set; }
[Column("CITY", TypeName = "varchar2"), MaxLength(25)]
public string City { get; set; }
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
}
控制器代码如下:
[EnableQuery]
public class StudentController : ODataController
{
DataContext db = new DataContext();
public IHttpActionResult Get()
{
return Ok();
}
}
WebApiConfig 代码如下:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Student>("Students");
builder.EntitySet<StudentAddress>("StudentAddresses");
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
}
我哪里做错了?
【问题讨论】:
-
你得到什么错误?
-
我更新了问题。
-
EnableQuery应该在Get方法上。将ODataRoute添加到Get 方法中。将ODataRoutePrefix("Students")添加到您现在拥有EnableQuery 的类定义中。将您的网址更改为http://localhost:7951/odata/Students(添加了一个 s)。 -
谢谢你的回答。你能给我更新代码吗?
-
@Demirline - 如果这对您有用,请考虑在 15 分钟期限到期后接受答案(请参阅 How to accept SO answers)。
标签: asp.net-mvc entity-framework asp.net-web-api odata