我正在制作一个web应用程序和web api。我已经在我的webapi中做到了这一点
public void Post([FromBody] Expense expense)
{
string commandText = "INSERT INTO EXPENSE (Date, Description,Rate, BranchId) VALUES " +
" (@Date, @Description, @Rate, @BranchId)";
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connStr))
{
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@Date", (DateTime)expense.Date);
command.Parameters.AddWithValue("@Description", (string)expense.Description);
command.Parameters.AddWithValue("@Rate", (Decimal)expense.Rate);
command.Parameters.AddWithValue("@BranchId", (int)expense.BranchId);
int rowInserted = command.ExecuteNonQuery();
connection.Close();
}
}
}
当我将所有参数从我的web应用程序发送到我的web api时,它运行良好,但rate和branch id是可选的。如果我不发送branchid和rate,那么它会给我错误“post/405(方法不允许)”。我可以做什么使参数可选。输入是json格式的。
ManageExpense() {
//object is to post data in JSON format to API
let object: any = {};
object.Date = this.Expense.Date;
object.Description = this.Expense.Description;
object.Rate = this.Expense.Rate;
object.BranchId = this.Expense.Branch.Id;
this.httpService.post('https://localhost:44373/api/add', object)
.then(res => {
console.debug(res);
});
return this.router.navigateToRoute("home");
}
即使在前端,我也得到错误,如果我没有发送所有参数
用于执行内联脚本,因为它违反了以下内容安全策略指令:“default src'self'”。启用内联执行需要'unsafe inline'关键字、哈希('sha256-thhi8uasfebbl6cisizpnj4z44unsq2tpkgyrtd3lyu=')或nonce('nonce-…')。另外请注意,没有显式设置“script src”,因此使用“default src”作为回退。
暂无答案!
目前还没有任何答案,快来回答吧!