Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在angular 4中从httppost方法将整数值传递给控制器?_Angular_Asp.net Web Api - Fatal编程技术网

如何在angular 4中从httppost方法将整数值传递给控制器?

如何在angular 4中从httppost方法将整数值传递给控制器?,angular,asp.net-web-api,Angular,Asp.net Web Api,在我们的Angular4MVC应用程序中,我们使用http服务从数据层获取数据。 请参阅代码片段 this.http.post(_this.baseUrl + "api/User/" + user.UserId) 在API控制器中,我没有获得UserId值 如果我传递一个字符串值,那么没有问题 请任何人帮助解决此问题。比方说,您得到了如下结果: @POST @Path("/deleteFile/{userId}/{fileName}") @Produces(MediaType.APPLICAT

在我们的Angular4MVC应用程序中,我们使用http服务从数据层获取数据。 请参阅代码片段

this.http.post(_this.baseUrl + "api/User/" + user.UserId)
在API控制器中,我没有获得UserId值

如果我传递一个字符串值,那么没有问题


请任何人帮助解决此问题。

比方说,您得到了如下结果:

@POST
@Path("/deleteFile/{userId}/{fileName}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public boolean deleteFile(@PathParam("userId") Integer userId, @PathParam("fileName") String fileName);
public deleteFile( userId: number, fileName: string ): Observable<boolean> {
    const headers = new Headers( { 'Content-Type': 'application/json' } );
    /* The option withCredentials: true is used for the CORS variant, that is, when our REST server is on a different WEB server than our node.js server, where my Angular2 development application is.
     */
    const options = new RequestOptions( { headers: headers, withCredentials: true } );

    return this.http.post( this.url + '/deleteFile/' + userId + '/' + fileName, null, options )
        .map( this.extractData )
        .catch( this.handleError );
}
那么服务功能应该如下所示:

@POST
@Path("/deleteFile/{userId}/{fileName}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public boolean deleteFile(@PathParam("userId") Integer userId, @PathParam("fileName") String fileName);
public deleteFile( userId: number, fileName: string ): Observable<boolean> {
    const headers = new Headers( { 'Content-Type': 'application/json' } );
    /* The option withCredentials: true is used for the CORS variant, that is, when our REST server is on a different WEB server than our node.js server, where my Angular2 development application is.
     */
    const options = new RequestOptions( { headers: headers, withCredentials: true } );

    return this.http.post( this.url + '/deleteFile/' + userId + '/' + fileName, null, options )
        .map( this.extractData )
        .catch( this.handleError );
}
publicdeletefile(userId:number,fileName:string):可观察{
const headers=新的头({'Content Type':'application/json'});
/*选项withCredentials:true用于CORS变体,也就是说,当我们的REST服务器与node.js服务器(我的Angular2开发应用程序所在的位置)位于不同的WEB服务器上时。
*/
const options=newrequestoptions({headers:headers,withCredentials:true});
返回此.http.post(this.url+'/deleteFile/'+userId+'/'+fileName,null,options)
.map(this.extractData)
.接住(这个.把手错误);
}

这是我的代码的一个简单示例,我在不久前用于我的应用程序,它应该可以工作。

根据您正在使用的URL,要进行后期调用,您所要做的就是创建控制器操作,如下所示:-

[HttpPost]
public IActionResult Post([FromRoute]int userId)
{
    var id = userId;
    return Ok();
}
如果用户id是字符串,则

[HttpPost]
public IActionResult Post([FromRoute]string userId)
{
    var id = userId;
    return Ok();
}
试试这个

this.http.post(_this.baseUrl + "api/User/MethodName?userId=1");
控制器

[HttpGet]
public string MethodName(long userId)
{
     //do you operation
}

请通过安装一个或类似的客户端来检查url是否正确

请发布整个控制器和服务好吗?这里我得到的用户ID为0。有什么建议吗?用户id的值是多少?是int还是string?如果它是int,那么它应该工作如果它是string,那么将int类型更改为string。我已经更新了答案