使用 axios.post 将一个简单的值 int 发送到我在 ASP.NET Core 上的控制器,当发送任何值时,方法控制器接收值“0”。
使用 axios 发送此类值的正确方法是什么(发布或删除)?
PD:我可以使用 [FromBody] 正确发送模型并在控制器上接收
方法控制器:
[Route("Delete"),HttpPost]
public async Task<ActionResult> Delete(int id)
{
try{
var result = await userService.DeleteUserPerson(id);
return Json(new{
response=true,
data=result,
error=""
});
}
catch(Exception ex){
return Json(new{
response=false,
data=false,
error=ex.Message
});
}
}
Run Code Online (Sandbox Code Playgroud)
反应类中的方法:
async function DeleteUser(id, props){
var request= new Request({});
try{
console.log(id);
var axiosResp= await request.axios_request.post('User/Delete', JSON.stringify({id:id}));
if(axiosResp.status!=200){
//do smething
}
//this case validate error
if(axiosResp.data.response && !axiosResp.data.error){
//do something
}
//do something
}catch(err){ …Run Code Online (Sandbox Code Playgroud) 在我的 .net core Api 中,我使用 Secrets.json:
{"UserServiceSecretKey": "secretKey123456"}
Run Code Online (Sandbox Code Playgroud)
显然在我的 .csproj 中:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>6da803bf-439d-4e34-8735-195d652e8366</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
并在我的 Startup.cs ConfigureServicesMethod() 中使用:
var secretKey = Configuration["UserServiceSecretKey"];
if (string.IsNullOrEmpty(secretKey))
Console.WriteLine("Error: KEY UserServiceSecretKey cannot be null...");
Run Code Online (Sandbox Code Playgroud)
如果在 IISExpres 上运行该应用程序,它就可以工作(获取密钥)。
但是如果我像 docker-compose 这样在 docker 中运行 Api,那么在运行时将无法获得密钥:

在我的docker-compose.override文件中,我有:
tresfilos.users.service:
environment:
- ASPNETCORE_ENVIRONMENT= Development
- ASPNETCORE_URLS= https://+:443;http://+:80
ports:
- "7002:80"
- "7003:443"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
Run Code Online (Sandbox Code Playgroud)
另外,我定义了 APPDATA 环境变量:
当我在 docker 中运行 Api 时如何访问密钥?
environment-variables docker docker-compose asp.net-core docker-secrets