У мене є таке
[EnableQuery]
public IQueryable<AccountViewModel> GetAccounts()
{
return _accountRepository.Table
.Select(x => new Bepoz.Api.Models.AccountViewModel() {Title = x.Title, AccountID = x.AccountID}).AsQueryable();
}
І
[EnableQuery]
public SingleResult<AccountViewModel> GetAccount([FromODataUri] int key)
{
return SingleResult.Create(this._accountRepository.Table.Where(account => account.AccountID == key)
.Select(x => new Bepoz.Api.Models.AccountViewModel() { Title = x.Title, AccountID = x.AccountID }).AsQueryable());
}
У моєму WebApiConfig
public static void Register(HttpConfiguration config)
{
// New code:
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<AccountViewModel>("Accounts");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: builder.GetEdmModel());
}
Коли я виконую
http://localhost:55045/odata/Accounts
Відповідь - всі рахунки, як очікувалося, однак, коли я виконую
http://localhost:55045/odata/Accounts(1)
я отримав
{
"error":{
"code":"","message":"No HTTP resource was found that matches the request URI "http://localhost:55045/odata/Accounts(1)".","innererror":{
"message":"No routing convention was found to select an action for the OData path with template "~/entityset/key".","type":"","stacktrace":""
}
}
}
Моя AccountViewModel виглядає наступним чином
public class AccountViewModel
{
[Key]
public int AccountID { get; set; }
public string Title { get; set; }
}
FYI - Якщо я зміню клас з AccountViewModel на акаунт, він буде працювати! Дякую
Відповіді:
1 для відповіді № 1Єдине, що потрібно зробити, це додати ще 2 рядки:
ODataModelBuilder builder = new ODataConventionModelBuilder();
EntitySetConfiguration<AccountViewModel> accounts= builder.EntitySet<AccountViewModel>("Accounts");
EntityTypeConfiguration<AccountViewModel> account = accounts.EntityType;
account.Name = "Account";