如何将对象从Angular发送到Asp.net Web Api?

我的Web Api中有例外

  

对象引用未设置为对象的实例。

对象中的数据将打印到控制台上,但不会传递到Api控制器。对象ae的“空”

这是我的控制者:

[Route("api/Addmovie")]
[HttpPost]
public int Addmovie([FromBody]Movie movie)
{
   int check = objMovie.Addmovie(movie);
   return check;
}

Addmovie是我创建的用于将数据存储在数据库中的功能。

这是我的组件:

import { FormGroup,FormControl,Validators,FormBuilder } from '@angular/forms';
import { slideInOutAnimation } from 'src/app/animations';
import { Movie } from 'src/app/movie';
import { NgxSpinnerService} from 'ngx-spinner';
import { MovieServiceService } from 'src/app/Service/movie-service.service';
import { HttpClient } from '@angular/common/http';
import { formatNumber } from '@angular/common';

@Component({
  selector: 'app-addmovie',templateUrl: './addmovie.component.html',styleUrls: ['./addmovie.component.css'],// make slide in/out animation available to this component
  animations: [slideInOutAnimation],// attach the slide in/out animation to the host (root) element of this component
  // tslint:disable-next-line: no-host-metadata-property
  host: { '[@slideInOutAnimation]': '' }
})
export class AddmovieComponent implements OnInit {

  // tslint:disable-next-line: new-parens
  movie = new Movie;
  fileData: File = null;
  addmovieForm: FormGroup;


  constructor( 
    private spinner: NgxSpinnerService,public fb: FormBuilder,private http: HttpClient,public movieService: MovieServiceService) {
    this.addmovieForm = this.fb.group ({
      movieName: new FormControl('',[Validators.required,Validators.minLength(1)]),releaseDate: new FormControl('',[Validators.required]),releaseYear: new FormControl('',certification: new FormControl('',runtime: new FormControl('',rating: new FormControl('',Validators.max(10)]),moviePlot: new FormControl('',cast: new FormControl('',imageName: new FormControl('',[Validators.required])
    });
  }

  ngOnInit() {
  }

  onFileSelect(event) {
    if (event.target.files.length > 0) {
      const file = event.target.files[0];
      this.addmovieForm.get('imageName').setvalue(file);
    }
  }
  public onSubmit() {
    debugger;
    // tslint:disable-next-line: prefer-const
    let movieForm = this.addmovieForm.value;
    this.movie.movieName = movieForm.movieName;
    this.movie.releaseDate = movieForm.releaseDate;
    this.movie.releaseYear = movieForm.releaseYear;
    this.movie.certification = movieForm.certification;
    this.movie.runtime = movieForm.runtime;
    this.movie.review = movieForm.rating;
    this.movie.moviePlot = movieForm.moviePlot;
    this.movie.cast = movieForm.cast;
    this.movie.imageName = movieForm.imageName;
    console.log(this.movie);
    this.http.post('http://localhost:50686/api/Addmovie',this.movie).subscribe(
      (res) => console.log(res),(err) => console.log(err)
    );
}
}
frazer999 回答:如何将对象从Angular发送到Asp.net Web Api?

您正在调用没有任何标头的api调用。 尝试以下

const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    };
this.http.post('http://localhost:50686/api/AddMovie',this.movie,httpOptions)
.subscribe(
      (res) => console.log(res),(err) => console.log(err)
    );
,

该错误表明您的对象Movie为空。这意味着您尚未初始化对象或将其设置为null。在这种情况下,您可以执行以下操作,

  1. 要在使用对象之前对其进行初始化,

    Movie movie = new Movie();
    
  2. 在使用对象实例之前检查null,

    public int AddMovie([FromBody]Movie movie)
    {
        if (movie != null) {
            // Do something with movie
        }
    } 
    
  3. 处理null并抛出适当的异常,

    public int AddMovie([FromBody]Movie movie)
    {
        if (movie == null) {
            throw new Exception("Custom exception message");
        }
    }
    
本文链接:https://www.f2er.com/3068657.html

大家都在问