C# 从Post请求Angular/.NET Core获取数据
从.Net核心服务器的POST请求中获取JSON响应时遇到问题。本质上,我将使用这个POST请求,就像从服务器获取请求一样。我相信我传递的是正确的标题,但是,在我的控制台错误中 错误类型错误:无法读取未定义的属性“sessionId” 我怀疑这与类型和/或型号有关。或者我在服务中如何称呼它。如果我需要补充任何澄清,lmk .NET核心服务器代码 行动 } ActionService.cs 动作控制器 角度客户端代码 动作部件 行动服务C# 从Post请求Angular/.NET Core获取数据,c#,angular,asp.net-core,.net-core,C#,Angular,Asp.net Core,.net Core,从.Net核心服务器的POST请求中获取JSON响应时遇到问题。本质上,我将使用这个POST请求,就像从服务器获取请求一样。我相信我传递的是正确的标题,但是,在我的控制台错误中 错误类型错误:无法读取未定义的属性“sessionId” 我怀疑这与类型和/或型号有关。或者我在服务中如何称呼它。如果我需要补充任何澄清,lmk .NET核心服务器代码 行动 } ActionService.cs 动作控制器 角度客户端代码 动作部件 行动服务 将[FromBody]添加到控制器端服务,在参数之前。Pos
将[FromBody]添加到控制器端服务,在参数之前。Post方法在主体中传递参数 像
我不知道为什么微软没有决定做这个默认设置。你应该从数据中删除{}:{}
通过这样做,您将为该对象分配一个空对象如果从您的http post调用中删除params单词会怎么样 所以http post调用是这样的
return this.http.post<Action[]>(this.apiURL + '/actions/launchactions',
{
sessionId: action.sessionId,
tag: action.actionTag,
actionParams: action.actionParams
})
.pipe(
retry(1),
catchError(this.handleError)
);
谢谢仍然出现错误,无法读取未定义的属性“sessionId”。不知道我在干什么missing@KoryJ.Campbell哦,我明白了,你连控制器都没到。但您也需要将其添加到控制器中。在action component上,您是否检查了this.currentAction是否真的已分配或为空?是的,似乎我遗漏了一些无法到达服务器的内容。@KoryJ.Campbell on action component,您是否检查了this.currentAction是真的赋值的还是空的?currentAction返回为未定义的。谢谢您的提示。我仍然收到我的原始错误,但你们在给post打电话之前检查过电流值了吗?如果您使用post获取值,您应该在正文中传递数据,而不是在paramsAny update中传递数据…我仍然需要这方面的帮助我相信这可能有帮助,但我的问题可能是因为这个。currentAction返回为未定义?可能是。从您提供的代码段中不清楚您是否已根据您提供的更新代码段分配currentAction或notbased,当您将currentAction的值传递给操作服务时,该值似乎为null。在调用此函数之前,您需要分配currentAction的值。Ngoninit中的getActions刚刚更新了代码;但我认为它会被分配到服务中,还是我错了?我以为我是在服务中分配的我不认为你在动作组件的代码中分配过这个值。因此,从技术上讲,当调用getActions时,会传入一个null对象。在Action服务的代码中,您实际上尝试将currentAction的值作为请求主体读取到.Net核心服务器。但由于currentAction为null,控制台将打印该错误。
{
ActionResponse LaunchAction(string sessionId, Tag actionTag, ActionParams args, UserState userState);
}
public IActionResult LaunchAction([FromBody]ActionDto launchActionParameters)
{
var sessionId = launchActionParameters.SessionId;
var actionTag = launchActionParameters.ActionTag;
var args = launchActionParameters.Args;
UserState userState = null;
RunAction runAction = null;
export interface ActionView {
actionName: string;
actionType: string;
primaryTable: string;
specialUse: string;
folder: string;
actionDescription: string;
actionTag: number;
chartType: string;
priority: number;
}
const ACTION_DATA: ActionView[] = [];
@Component({
templateUrl: 'home.component.html'
})
export class HomeComponent implements OnInit, OnDestroy {
// User Fields
currentUser: User;
users: User[] = [];
currentUserSubscription: Subscription;
// Action Fields
currentAction: Action;
actions: Action[] = [];
displayedColumns: string[] =
['actionName', 'actionType', 'primaryTable', 'specialUse',
'folder', 'actionDescription', 'actionTag', 'chartType',
'priority'];
dataSource: any = new MatTableDataSource(ACTION_DATA);
constructor(
private authenticationService: AuthenticationService,
private iconRegistry: MatIconRegistry,
private sanitizer: DomSanitizer,
private httpClient: HttpClient,
private actionService: ActionService
) {
this.currentUserSubscription = this.authenticationService.currentUser.subscribe(user => {
this.currentUser = user;
});
this.iconRegistry.addSvgIcon(
'thumbs-up',
this.sanitizer.bypassSecurityTrustResourceUrl('assets/img/examples/thumbup-icon.svg'));
}
@ViewChild(MatSort) sort: MatSort;
public getActions() {
console.log('test');
this.actionService.getActions(
this.currentAction).subscribe((data) => {
this.dataSource = data;
});
}
ngOnInit() {
this.dataSource.sort = this.sort;
this.getActions();
}
ngOnDestroy() {
// unsubscribe to ensure no memory leaks
this.currentUserSubscription.unsubscribe();
}
}
@Injectable({ providedIn: 'root' })
export class ActionService {
public apiURL = 'http://localhost:15217/api';
public currentUser: Observable<User>;
public currentAction: Observable<Action>;
constructor(private http: HttpClient) { }
// Http Options
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
getActions(action: Action): Observable<Action[]> {
return this.http.post<Action[]>(this.apiURL + '/actions/launchactions',
{
sessionId: action.sessionId,
tag: action.actionTag,
actionParams: action.actionParams
})
.pipe(
retry(1),
catchError(this.handleError)
);
}
// Error handling
handleError(error: any) {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// Get client-side error
errorMessage = error.error.message;
} else {
// Get server-side error
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
}
window.alert(errorMessage);
return throwError(errorMessage);
}
ActionResponse LaunchAction([FromBody]string sessionId, [FromBody]Tag actionTag, [FromBody]ActionParams args, [FromBody]UserState userState);
return this.http.post<Action[]>(this.apiURL + '/actions/launchactions',
{
sessionId: action.sessionId,
tag: action.actionTag,
actionParams: action.actionParams
})
.pipe(
retry(1),
catchError(this.handleError)
);