我试图以这种方式在.NET Core中启用CORS:
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseCors("AllowAll");
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我向Angular 2发送请求到我的应用程序时,我得到了名人
"请求的资源上没有'Access-Control-Allow-Origin'标头."
错误信息.
我也使用Windows身份验证+ WebListener.如果我与邮递员核对,唯一的响应标题是:
Content-Length→3533 Content-Type→application/json; charset = utf-8日期→星期五,2016年10月14日12:17:57 GMT服务器→Microsoft-HTTPAPI/2.0
所以必须仍然配置错误.有什么建议?
如果我删除了outcommented行它,但我需要Windows身份验证:-(
var host = new WebHostBuilder()
.UseWebListener()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
//.UseWebListener(options => options.Listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM)
.Build();
Run Code Online (Sandbox Code Playgroud) 我有一个服务,很多我的Angular 2组件使用过多次.它从Web API获取客户数据并返回Observable:
getCustomers() {
return this.http
.get(this.baseURI + this.url)
.map((r: Response) => {
let a = r.json() as Customer[];
return a;
});
}
Run Code Online (Sandbox Code Playgroud)
我在我的根组件中注入了这个服务,在每个想要访问客户的组件中,我只订阅了Observable:
this.customerService.getCustomers().subscribe(v => this.items = v);
Run Code Online (Sandbox Code Playgroud)
但是,订阅我的Observable的每个组件都会导致另一个HTTP请求的执行.但是只获取一次数据就足够了.如果我尝试share(),它不能解决我的问题:
getCustomers() {
return this.http
.get(this.baseURI + this.url)
.map((r: Response) => {
let a = r.json() as Customer[];
return a;
}).share();
}
Run Code Online (Sandbox Code Playgroud)
仍然是同一个问题.操作员必须使用的任何建议只能获取一次数据?
我尝试用谷歌搜索它,但在所有示例中都没有得到很好的解释。在以下上下文中,EntitySet() 方法到底执行什么操作?
private static IEdmModel GetEdmModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.ContainerName = "DefaultContainer";
builder.EntitySet<HouseEntity>("Houses");
return builder.GetEdmModel();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,它允许从该服务中使用 HouseEntity 对象。但参数“Houses”代表什么?我首先想到的是,这是控制器的名称,但这似乎不是真的。路线名称?班级名称?字符串参数代表什么?
谢谢 :)
考虑这个例子:
if(this.plantService.plants[id])
{
if(this.plantService.plants[id].Name)
{
if(this.plantService.plants[id].Name[0])
return this.plantService.plants[id].Name[0].value;
else
return '';
}
else
return '';
}
return '';
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能简化我在这里做的事情.
我的目标是测试对象链this.plantService.plants[id].Name[0]的有效性.
但是,如果我只是测试if(this.plantService.plants[id].Name[0]) {...}异常被抛出.
有什么建议?:)
昨天我想,我发布的 WebAPI 不是很快。通常每个请求在本地环境(发布模式)中大约需要 300 毫秒。为了确保我自己的代码不是问题,我在 Visual Studio 2017 中创建了一个新的 Web 应用程序并选择了“WebAPI”项目。当我以发布模式启动新项目并请求仅返回 ["value1","value2"] 的值控制器时,我的响应时间在 50 到 100 毫秒之间波动。如果我创建一个新的 WebAPI .NET Core 项目,我会得到更糟糕的结果:值介于 100 到 200 毫秒之间。对我来说,对于本地环境和获取如此少量的数据,这些结果非常糟糕。
如果您创建一个新项目 - 是响应时间:
有什么建议可以尝试加速我的 API。我不敢相信有时需要 200 毫秒才能请求字面上少于 1 千字节。
我的意思是:即使是这个在线 API 也快得多并返回更多数据:https : //jsonplaceholder.typicode.com/posts(大约 20 毫秒)
.net ×2
.net-core ×2
javascript ×2
angular ×1
asp.net ×1
asp.net-core ×1
c# ×1
cors ×1
observable ×1
odata ×1
rxjs ×1
web ×1