Aurelia提取客户端 - 不支持的媒体类型 - .NET CORE WEBAPI



当我尝试使用aurelia fetch客户端将两个参数发布为.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] IT帖子,但是ApplicationSearchModel中的属性为null。

当我使用以下设置与Postman发布时:

url:

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

身体:

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

标题:

Content-Type: application/json

一切都起作用,我的 ApplicationSearchModel内部的属性都不为空。

当我查看Aurelia Fetch客户端生成的请求时,似乎缺少内容类型的标题。

编辑

请求标头:

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

..这实际上是一个问题。

我在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应用程序中提供静态页面,但现在正在开发中,我使用端口:9000 ,因为Aurelia提供了浏览器,(CORS在发布时不会发表时出现问题,但现在是使用端口时:本地9000)。

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

编辑:

仅在本地主机上启用CORS:

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

您的标头无法正确设置。

应该是

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

而不是

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

最新更新