angular - 对数据库的全局访问不起作用
问题描述
我在这里进行了 WebAPI 和 Angular 的协作 - https://angular.io/tutorial 但他们自己在那里填补了英雄。我需要从我已经创建的数据库中获取名称。WebAPI 中的请求有效。当我通过 webapi 项目去 api / 剧院时,我输出正确。
所以我需要你的建议。那么,我是如何让全球数据库无法使用的呢?
如果我在 theatre.service 文件中执行以下操作 -
export class TheatresService {
private theatreUrl = 'api/Theatre';
那么什么都行不通
如果我这样说——
export class TheatresService {
private theatreUrl = 'api/theatres';
我扔在文件中,它将正确显示。
带有剧院 in-memory-data-service.ts 的文件
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { Theatre } from './theatres.model';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class InMemoryDataServiceTheatre implements InMemoryDbService {
createDb() {
const theatres = [
{ id: 11, name: 'theatre' },
{ id: 12, name: 'opera'},
{ id: 13, name: 'operatta' },
];
return {theatres};
}
}
和 app.module.ts
@NgModule({
declarations: [
.......
],
imports: [
........
HttpClientModule,
HttpClientInMemoryWebApiModule.forRoot(
InMemoryDataServiceTheatre, {dataEncapsulation: false}
)
],
providers: [TheatresService],
bootstrap: [AppComponent]
})
export class AppModule { }
告诉我有什么问题。或删除显示如何使用内部数据库的链接
更新我的模型
namespace TicketSalePoint.Data.Models
{
[Table("Theatres")]
public class Theatre
{
public Theatre() { }
public int Id { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public DateTime RegistrationDate { get; set; }
public virtual ICollection<Show> Shows { get; set; }
}
}
我的带有 webAPI 的控制器
namespace TicketSalePointWebAPI.Controllers
{
[AllowAnonymous]
public class TheatreController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> GetNames()
{
using (var dataBaseContext = new DataBaseContext())
{
dataBaseContext.Database.Connection.Open();
var query = from theatre in dataBaseContext.Theatres
select new { theatre.Id, theatre.Name };
return Ok(await query.ToListAsync());
}
}
}
}
我看到错误:
{body: {…}, url: "api/Theatre", headers: HttpHeaders, status: 404, statusText: "Not Found"}
body: {error: "Collection 'Theatre' not found"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 404
statusText: "Not Found"
url: "api/Theatre"
__proto__: Object
解决方案
推荐阅读
- python - 为什么使用 3d 数组的 ndimage.convolve 会产生 3d 数组而不是 2d?
- drools - Drools - 何时使用多个 kie 会话?
- javascript - mongodb 找不到匹配项
- javascript - 试图为我的机器人制作一个不和谐的歌曲队列
- git - 基于 yaml 管道上的资源的条件
- video-streaming - WebRTC - WebRTC: ICE failed, add a STUN server and see about:webrtc 了解更多详情
- asp.net-mvc - 如何在使用 ASP.NET MVC 的 Plesk 托管中使用 Rotativa 生成 PDF?
- javascript - A4 页面中 Array 的 Angular Splitting Object 它工作起来很奇怪,但并不例外
- xml - 如何在 xslt 中选择名称和相应的内容?
- javascript - 在 laravel 刀片中显示 JSON 数组