Aurelia获取客户端-不支持的媒体类型-.NET核心WebAPI

Aurelia获取客户端-不支持的媒体类型-.NET核心WebAPI,.net,asp.net-core,.net-core,aurelia,aurelia-fetch-client,.net,Asp.net Core,.net Core,Aurelia,Aurelia Fetch Client,当我尝试使用Aurelia Fetch Client将两个参数作为JSON发布到.NET核心WebAPI时,我得到:415(不支持的媒体类型) 应用程序控制器 [HttpPost("getapplications")] public ApplicationViewModel GetApplications([FromBody] ApplicationSearchModel model) { var applications = _applicationService.GetApplica

当我尝试使用Aurelia Fetch Client将两个参数作为JSON发布到.NET核心WebAPI时,我得到:
415(不支持的媒体类型)

应用程序控制器

[HttpPost("getapplications")]
public ApplicationViewModel GetApplications([FromBody] ApplicationSearchModel model)
{
    var applications = _applicationService.GetApplications().ToList();

    return new ApplicationViewModel()
    {
        Applications = applications
    };
}

public class ApplicationSearchModel
{
    public DateTime? From { get; set; }

    public DateTime? To { get; set; }
}
application.js

import { inject } from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';

@inject(HttpClient)

export class Application {
  constructor(httpClient) {
    this.applications = [];

    this.httpClient = httpClient;
  }

  getApplications() {
    this.httpClient.fetch("http://localhost:9001/api/application/getapplications", {
      method: "POST",
      body: JSON.stringify({
        From: '2017-02-18',
        To: '2017-02-18'
      }),
        headers: {
        "content-type": "application/json; charset=utf-8" //Tried without headers aswell
      }
    });
  }

  activate(params) {
    this.getApplications();
  }
}
如果我删除
[FromBody]
它会发布,但我的
应用程序搜索模型中的属性为空

当我使用以下设置与邮递员一起发布时:

网址:

正文:

标题:

Content-Type: application/json
一切正常,我在
ApplicationSearchModel
中的属性不为空

当我查看Aurelia Fetch Client生成的请求时,似乎缺少内容类型头

编辑

请求标头:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:9001
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

您的标题设置不正确

应该是

{
    "content-type": "application/json; charset=utf-8" 
}
而不是

{
    "content-type", "application/json; charset=utf-8"
}

您的标题设置不正确

应该是

{
    "content-type": "application/json; charset=utf-8" 
}
而不是

{
    "content-type", "application/json; charset=utf-8"
}

…这实际上是CORS的问题

我在Startup.cs中添加了cors设置,以允许所有来源:

// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
    //CORS---------
    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
    });

    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    //CORS---------
    app.UseCors("CorsPolicy");

    loggerFactory.AddConsole();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();

    app.UseDefaultFiles();

    app.UseStaticFiles();

    // app.UseStaticFiles(new StaticFileOptions()
    // {
    //     FileProvider = new PhysicalFileProvider(
    //     Path.Combine(Directory.GetCurrentDirectory(), "src")),
    //     RequestPath = new PathString("/src")
    // });
}
一切都成功了

我的Aurelia应用程序托管在端口:9000上,而我的.NET应用程序托管在端口:90001上。这个想法是在我的.NET应用程序发布后提供静态页面,但由于Aurelia提供的BrowserSync,我现在正在开发中使用端口:9000(发布时CORS不会成为问题,但现在在本地使用端口:9000)

是否可以在本地使用端口:9000而不启用CORS? 编辑:

仅在本地主机上启用CORS:

app.UseCors(builder =>
{
    builder.WithOrigins("http://localhost:9000")
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials();
});

…这实际上是CORS的问题

我在Startup.cs中添加了cors设置,以允许所有来源:

// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
    //CORS---------
    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
    });

    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    //CORS---------
    app.UseCors("CorsPolicy");

    loggerFactory.AddConsole();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();

    app.UseDefaultFiles();

    app.UseStaticFiles();

    // app.UseStaticFiles(new StaticFileOptions()
    // {
    //     FileProvider = new PhysicalFileProvider(
    //     Path.Combine(Directory.GetCurrentDirectory(), "src")),
    //     RequestPath = new PathString("/src")
    // });
}
一切都成功了

我的Aurelia应用程序托管在端口:9000上,而我的.NET应用程序托管在端口:90001上。这个想法是在我的.NET应用程序发布后提供静态页面,但由于Aurelia提供的BrowserSync,我现在正在开发中使用端口:9000(发布时CORS不会成为问题,但现在在本地使用端口:9000)

是否可以在本地使用端口:9000而不启用CORS? 编辑:

仅在本地主机上启用CORS:

app.UseCors(builder =>
{
    builder.WithOrigins("http://localhost:9000")
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials();
});

是否启用了CORS?不,但它不是跨域请求。
415(不支持的媒体类型)
的原因正是您提到的:似乎缺少内容类型标题。我尝试了所有可能的方法添加内容类型,但它始终忽略我在启用CORS时所做的一切?不,但这不是跨域请求。
415(不支持的媒体类型)
的原因正是您所提到的:似乎缺少内容类型标题。我尝试了各种可能的方法添加内容类型,但它一直忽略我所做的一切,实际上是我编辑问题时的一个输入错误。这并不能解决我的问题。我编辑问题时,他实际上是个打字错误。不能解决我的问题