我如何将对象从 Angular 发送到 Asp.net Web Api?
How do i send an object from Angular to an Asp.net Web Api?
我的网站出现异常 Api
Object reference not set to an instance of an object.
对象中的数据打印到控制台,但未传递到 Api 控制器。对象 ae s "null"
这是我的控制器:
[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('' , [Validators.required]),
certification: new FormControl('' , [Validators.required]),
runtime: new FormControl('', [Validators.required]),
rating: new FormControl('', [Validators.required, Validators.max(10)]),
moviePlot: new FormControl('', [Validators.required, Validators.minLength(1)]),
cast: new FormControl('', [Validators.required, Validators.minLength(1)]),
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)
);
}
}
您正在调用 api 调用,但没有任何 header。
尝试以下
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
为空。这意味着你还没有初始化你的对象,或者你已经将它设置为空。在这种情况下,您可以执行以下操作,
在使用之前初始化对象,
Movie movie = new Movie();
在使用对象实例之前检查是否为空,
public int AddMovie([FromBody]Movie movie)
{
if (movie != null) {
// Do something with movie
}
}
处理 null
并抛出合适的异常,
public int AddMovie([FromBody]Movie movie)
{
if (movie == null) {
throw new Exception("Custom exception message");
}
}
我的网站出现异常 Api
Object reference not set to an instance of an object.
对象中的数据打印到控制台,但未传递到 Api 控制器。对象 ae s "null"
这是我的控制器:
[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('' , [Validators.required]),
certification: new FormControl('' , [Validators.required]),
runtime: new FormControl('', [Validators.required]),
rating: new FormControl('', [Validators.required, Validators.max(10)]),
moviePlot: new FormControl('', [Validators.required, Validators.minLength(1)]),
cast: new FormControl('', [Validators.required, Validators.minLength(1)]),
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)
);
}
}
您正在调用 api 调用,但没有任何 header。 尝试以下
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
为空。这意味着你还没有初始化你的对象,或者你已经将它设置为空。在这种情况下,您可以执行以下操作,
在使用之前初始化对象,
Movie movie = new Movie();
在使用对象实例之前检查是否为空,
public int AddMovie([FromBody]Movie movie) { if (movie != null) { // Do something with movie } }
处理
null
并抛出合适的异常,public int AddMovie([FromBody]Movie movie) { if (movie == null) { throw new Exception("Custom exception message"); } }