【问题标题】:Asp.Net Core 3.1 post request doesn't work with Angular 8 on serverAsp.Net Core 3.1 发布请求不适用于服务器上的 Angular 8
【发布时间】:2020-11-03 10:01:15
【问题描述】:

它适用于本地或邮递员请求。但是当我发布文件时,它会出现“未知错误”。我正在使用 Windows 身份验证。我已经从 Firefox 尝试过,然后我在下面收到此错误: firefox error 。当我接受 Firefox 原始请求并粘贴到 Postman 时,它就起作用了。

这是我的控制器方法:

    [HttpPost]
    public ApiResult<OverTimePlanning> Post([FromBody]OverTimePlanning overTimePlanning)
    {
        logger.LogInformation("Planning Add started");
        var result = new OverTimePlanning();
        var apiResult = new ApiResult<OverTimePlanning>();
        ... Some codes are here ...
        logger.LogInformation("Planning Add ended");
        return apiResult;
    }

这是我的 C# 模型类:

public class OverTimePlanning
{
    public int ID { get; set; }
    public Guid GUID { get; set; }
    public string Unit { get; set; }
    public DateTime DateStarted { get; set; }
    public DateTime DateEnded { get; set; }
    public int? ApproverUserId { get; set; }
    public string ApproverUser { get; set; }
    public string ApproverUserPhoto { get; set; }
    public string Description { get; set; }
    public string ShortDescription { get; set; }
    public OnayDurumu Status { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? DateUpdated { get; set; }
    public int UserCreatedId { get; set; }
    public string UserCreated { get; set; }
    public string UserPhoto { get; set; }
    public string ApproverComment { get; set; }
    /// <summary>
    /// Employee list
    /// </summary>
    public IEnumerable<OrganisationUser> Employees { get; set; } 
}

public class OrganisationUser
{
    public int ID { get; set; }
    public int EmployeeID { get; set; }
    public string Name  { get; set; }
    public string ShortName { get; set; }
    public string UserName { get; set; }
    public string Email  { get; set; }
    public string Photo    { get; set; }
    public string Manager { get; set; }
    public string Unit { get; set; }
    public int Level { get; set; }
   
}

这是我的 Angular 服务代码:

saveOverTimePlanning(overtime: OverTimePlanning) {
const header = new HttpHeaders().set('Content-Type', 'application/json');
const options = { headers: header, withCredentials: true };
return this.http.post(this.apiUrl, JSON.stringify(overtime).replace(/_/g, ''), options);
}

这是我的 Angular 模型:

import { OrganisationUser } from './organisationUser.model'
export class OverTimePlanning {
private _id: number
public get id(): number {
    return this._id
}
public set id(value: number) {
    this._id = value
}
private _guid: string
public get guid(): string {
    return this._guid
}
public set guid(value: string) {
    this._guid = value
}
private _unit: string
public get unit(): string {
    return this._unit
}
public set unit(value: string) {
    this._unit = value
}
private _dateStarted: Date
public get dateStarted(): Date {
    return this._dateStarted
}
public set dateStarted(value: Date) {
    this._dateStarted = value
}
private _dateEnded: Date
public get dateEnded(): Date {
    return this._dateEnded
}
public set dateEnded(value: Date) {
    this._dateEnded = value
}
private _approverUserId: number
public get approverUserId(): number {
    return this._approverUserId
}
public set approverUserId(value: number) {
    this._approverUserId = value
}
private _approverUser: string
public get approverUser(): string {
    return this._approverUser
}
public set approverUser(value: string) {
    this._approverUser = value
}
private _approverUserPhoto: string
public get approverUserPhoto(): string {
    return this._approverUserPhoto
}
public set approverUserPhoto(value: string) {
    this._approverUserPhoto = value
}
private _description: string
public get description(): string {
    return this._description
}
public set description(value: string) {
    this._description = value
}
private _shortDescription: string
public get shortDescription(): string {
    return this._shortDescription
}
public set shortDescription(value: string) {
    this._shortDescription = value
}
private _status: number
public get status(): number {
    return this._status
}
public set status(value: number) {
    this._status = value
}
private _dateCreated: Date
public get dateCreated(): Date {
    return this._dateCreated
}
public set dateCreated(value: Date) {
    this._dateCreated = value
}
private _dateUpdated: Date
public get dateUpdated(): Date {
    return this._dateUpdated
}
public set dateUpdated(value: Date) {
    this._dateUpdated = value
}
private _userCreatedId: number
public get userCreatedId(): number {
    return this._userCreatedId
}
public set userCreatedId(value: number) {
    this._userCreatedId = value
}
private _userCreated: string
public get userCreated(): string {
    return this._userCreated
}
public set userCreated(value: string) {
    this._userCreated = value
}
private _userPhoto: string
public get userPhoto(): string {
    return this._userPhoto
}
public set userPhoto(value: string) {
    this._userPhoto = value
}
private _approverComment: string
public get approverComment(): string {
    return this._approverComment
}
public set approverComment(value: string) {
    this._approverComment = value
}

private _employees: OrganisationUser[]
public get employees(): OrganisationUser[] {
    return this._employees
}
public set employees(value: OrganisationUser[]) {
    this._employees = value
}

}

export class OrganisationUser {
private _id: number;
public get id(): number {
    return this._id;
}
public set id(value: number) {
    this._id = value;
}
private _employeeID: number;
public get employeeID(): number {
    return this._employeeID;
}
public set employeeID(value: number) {
    this._employeeID = value;
}
private _name: string;
public get name(): string {
    return this._name;
}
public set name(value: string) {
    this._name = value;
}
private _shortName: string;
public get shortName(): string {
    return this._shortName;
}
public set shortName(value: string) {
    this._shortName = value;
}
private _userName: string;
public get userName(): string {
    return this._userName;
}
public set userName(value: string) {
    this._userName = value;
}
private _email: string;
public get email(): string {
    return this._email;
}
public set email(value: string) {
    this._email = value;
}
private _photo: string;
public get photo(): string {
    return this._photo;
}
public set photo(value: string) {
    this._photo = value;
}
private _manager: string;
public get manager(): string {
    return this._manager;
}
public set manager(value: string) {
    this._manager = value;
}
private _unit: string;
public get unit(): string {
    return this._unit;
}
public set unit(value: string) {
    this._unit = value;
}
private _level_: number;
public get level_(): number {
    return this._level_;
}
public set level_(value: number) {
    this._level_ = value;
}
}

【问题讨论】:

    标签: asp.net angular post .net-core asp.net-core-webapi


    【解决方案1】:

    状态码为零通常是导致 CORS 错误的原因。检查 CORS 的服务器端实现。

    这是 .net 中 CORS 中间件的官方文档:https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1

    在启动时只包含中间件:(来自文档):

     readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(name: MyAllowSpecificOrigins,
                              builder =>
                              {
                                  builder.WithOrigins("http://example.com",
                                                      "http://www.contoso.com");
                              });
        });
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {       
        app.UseCors(MyAllowSpecificOrigins);
       
    }
    

    【讨论】:

    • 感谢您的回答,但我已经添加了这个 CORS 部分。对我来说这是一个不同的问题:) 当依赖项目引发错误时,全局错误处理中间件不会捕获异常。但是服务器在这种情况下返回了“CORS”错误。
    【解决方案2】:

    我学到了: 您不能使用 Windows 凭据发送原始/json 请求数据。因为最新的浏览器不允许使用 Windows 凭据进行预检 (HttpOptions) 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 2018-11-07
      相关资源
      最近更新 更多