C# 在6中传递带空字符串的整数值时未调用API Get方法
在angular应用程序中,从angular调用API方法时出错。我必须传递两个参数。第一个是整数值,第二个是字符串值。它是可选的 请参阅下面的代码片段(Typescript)C# 在6中传递带空字符串的整数值时未调用API Get方法,c#,angular,typescript,asp.net-core,asp.net-core-webapi,C#,Angular,Typescript,Asp.net Core,Asp.net Core Webapi,在angular应用程序中,从angular调用API方法时出错。我必须传递两个参数。第一个是整数值,第二个是字符串值。它是可选的 请参阅下面的代码片段(Typescript) let id:number=5; let值:string=“”; this.http.get(this.appService.baseUrl+'api/File/'+id+“/”+value) 在控制器中: [HttpGet("{id:int}/value")] [ResponseCache(NoStore = tru
let id:number=5;
let值:string=“”;
this.http.get(this.appService.baseUrl+'api/File/'+id+“/”+value)
在控制器中:
[HttpGet("{id:int}/value")]
[ResponseCache(NoStore = true)]
public async Task<IActionResult> Get(int id, string value) { }
[HttpGet(“{id:int}/value”)]
[响应缓存(NoStore=true)]
公共异步任务Get(int-id,字符串值){}
在这里,由于value参数为空,Get方法没有调用。我只对asp.net web api有经验,但据我所知,字符串值为空会导致路由更改。因为您没有只接受int的路由,所以您的请求永远不会到达api。您需要做的是重载Get方法,并使其只接受int。您在URL中输入了变量的类型
shareType
,并为变量shareValue
传递空字符串。删除变量shareType
后的:数字
,并为变量shareValue
指定一个值在您的示例中,您正在构建此URL:
/api/File/5
但是,您的控制器需要以下各项:
/api/File/5/value
如果希望此处的值
是可选的,并被放入值
参数(字符串值
),可以调整HttpGet
属性,如下所示:
[HttpGet("{id:int}/{value?}")]
这使得
value
成为可选的,并且是传递进来的任何内容的占位符。您能提到您收到的错误消息吗?
[HttpGet("{id:int}/{value?}")]