我正在尝试更新“游戏”表中的条目。但是,我在ASP.net中的放置请求似乎从未触发过,而且我也不知道为什么。
这是ASP.net中的控制器:
[Route("game/{update.GameID}")]
[HttpPut]
public IActionResult updateGame([FromBody]Game update)
{
var result = context.Games.SingleOrDefault(g => g.GameID == update.GameID);
if (result != null)
{
result = update;
context.SaveChanges();
}
return Created("", result);
}
这是我在Angular中使用的代码:
url:string;
constructor(private _http: HttpClient) {
this.url = "https://localhost:44359/api/v1/"
};
putGame(id:number, game:Game){
return this._http.put(this.url + "game/" + id, game);
}
编辑1:我确实有一个GET请求列表,它们都能正常工作。只是PUT请求失败。
答案 0 :(得分:2)
如果您使用import Foundation
import CommonCrypto
struct AES {
// MARK: - Value
// MARK: Private
private let key: Data
private let iv: Data
// MARK: - Initialzier
init?(key: String, iv: String) {
guard key.count == kCCKeySizeAES128 || key.count == kCCKeySizeAES256, let keyData = key.data(using: .utf8) else {
debugPrint("Error: Failed to set a key.")
return nil
}
guard iv.count == kCCBlockSizeAES128, let ivData = iv.data(using: .utf8) else {
debugPrint("Error: Failed to set an initial vector.")
return nil
}
self.key = keyData
self.iv = ivData
}
// MARK: - Function
// MARK: Public
func encrypt(string: String) -> Data? {
return crypt(data: string.data(using: .utf8), option: CCOperation(kCCEncrypt))
}
func decrypt(data: Data?) -> String? {
guard let decryptedData = crypt(data: data, option: CCOperation(kCCDecrypt)) else { return nil }
return String(bytes: decryptedData, encoding: .utf8)
}
func crypt(data: Data?, option: CCOperation) -> Data? {
guard let data = data else { return nil }
let cryptLength = [UInt8](repeating: 0, count: data.count + kCCBlockSizeAES128).count
var cryptData = Data(count: cryptLength)
let keyLength = [UInt8](repeating: 0, count: kCCBlockSizeAES128).count
let options = CCOptions(kCCOptionPKCS7Padding)
var bytesLength = Int(0)
let status = cryptData.withUnsafeMutableBytes { cryptBytes in
data.withUnsafeBytes { dataBytes in
iv.withUnsafeBytes { ivBytes in
key.withUnsafeBytes { keyBytes in
CCCrypt(option, CCAlgorithm(kCCAlgorithmAES), options, keyBytes, keyLength, ivBytes, dataBytes, data.count, cryptBytes, cryptLength, &bytesLength)
}
}
}
}
guard UInt32(status) == UInt32(kCCSuccess) else {
debugPrint("Error: Failed to crypt data. Status \(status)")
return nil
}
cryptData.removeSubrange(bytesLength..<cryptData.count)
return cryptData
}
}
请求,则需要添加资源ID来更新或创建新资源-因此,请勿将ID与对象结合在一起
let password = "UserPassword1!"
let key128 = "1234567890123456" // 16 bytes for AES128
let key256 = "12345678901234561234567890123456" // 32 bytes for AES256
let iv = "abcdefghijklmnop" // 16 bytes for AES128
let aes128 = AES(key: key128, iv: iv)
let aes256 = AES(key: key256, iv: iv)
let encryptedPassword128 = aes128?.encrypt(string: password)
aes128?.decrypt(data: encryptedPassword128)
let encryptedPassword256 = aes256?.encrypt(string: password)
aes256?.decrypt(data: encryptedPassword256)
如果您使用的是 Asp.net Core ,则可以像上面的代码一样在HTTP谓词属性上重新编写您的URL-在URL中传递资源ID并将对象绑定到正文-您的网址应为PUT
希望这对您有帮助-编码愉快!!
答案 1 :(得分:1)
路由模板参数{update.GameID}
不是文档建议的标准
假设游戏ID是整数,请查看以下内容
//PUT .../game/5
[Route("game/{id:int}")]
[HttpPut]
public IActionResult updateGame(int id, [FromBody]Game update) {
//...
}
引用Routing to controller actions in ASP.NET Core
我还建议您检查该操作的逻辑,因为我不认为该操作在更新从上下文返回的实体方面发挥了您的作用。
答案 2 :(得分:0)
您能否像修改
一样修改定义路线[Route("game")]
[HttpPut]
public IActionResult updateGame([FromBody]Game update)
{
//your code
}
然后从像这样的角度拨打电话
putGame(game:Game){
return this._http.put(this.url + "game", game);
}
您可以从游戏对象接收gameid,因此不需要从url