Aurelia Fetch 客户端 - 不支持的媒体类型 - .NET Core WebAPI

Aurelia Fetch Client - Unsupported Media Type - .NET Core WebAPI

当我尝试 post 两个参数作为 JSON 使用 Aurelia Fetch Client 到 .NET Core WebAPI 我得到:415 (Unsupported Media Type)

ApplicationController

[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] 它 posts 但我的 ApplicationSearchModel 中的属性是空的。

当我使用以下设置与 Postman 进行 post 编辑时:

Url:

http://localhost:9001/api/application/getapplications

Body:

{
    "from": "2017-02-18",
    "to": "2017-02-18"
}

Header:

Content-Type: application/json

一切正常,我在 ApplicationSearchModel 中的属性不为空。

当我查看 Aurelia Fetch Client 生成的请求时,似乎缺少 Content-Type header..

编辑

请求Headers:

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

您的 headers 设置不正确。

应该是

{
    "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 应用程序托管在 port:9000 上,我的 .NET 应用程序托管在 port:90001 上.我的想法是在应用程序发布后从我的 .NET 应用程序提供静态页面,但现在在开发中我使用 port:9000 因为 Aurelia 提供的 BrowserSync,(CORS 不会发布时是个问题,但现在在本地使用 port:9000 时是个问题。

是否可以在本地使用 port:9000 而不启用 CORS?

编辑:

仅在本地主机上启用 CORS:

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