[英]How do I access the variable of one method from another method within the same class C#?
我正在研究 Asp.net 项目。 我创建了一个表单,它要求输入connectionId
和holderFirstName
。 asp-action 的名称是SendProofNameRequest
。
在 Controller 中,我写了一个方法SendProofNameRequest
,它以connectionId
和holderFirstName
作为参数。 但问题是,我将holderFirstName
作为 Input 的目的是在另一个方法( VerifyFirstName
)中使用它。
所以,我的问题是如何将holderFirstName
作为用户的输入并在另一种方法 / VerifyFirstName
(不是SendProofNameRequest
)中使用它。
详细信息.cshtml
<form class="input-group mb-3" asp-controller="Proof" asp-action="SendProofNameRequest">
<input type="hidden" name="connectionId" value="@Model.Connection.Id" />
<input type="text" name="holderFirstName" autocomplete="off" class="form-control" placeholder="Enter First Name" aria-label="First Name" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-outline-info" type="submit">Request a Proof of First Name</button>
</div>
</form>
证明控制器.cs
[HttpPost]
public async Task<IActionResult> SendProofNameRequest(string connectionId, out string holderFirstName)
{
var agentContext = await _agentProvider.GetContextAsync();
var connectionRecord = await _walletRecordService.GetAsync<ConnectionRecord>(agentContext.Wallet, connectionId);
var proofNameRequest = await CreateProofNameMessage(connectionRecord);
await _messageService.SendAsync(agentContext.Wallet, proofNameRequest, connectionRecord);
return RedirectToAction("Index");
}
验证名字方法
我想用holderFirstName
(动态值/用户在表单中输入)替换firstname
(静态值)
public bool VerifyFirstName(PartialProof proof)
{
var firstName = "Fyodor";
var name = proof.RequestedProof.RevealedAttributes.First();
if (name.Value.Raw.Equals(firstName))
{
return true;
}
return false;
}
更新
正如你所说的添加模型,我做到了......在ViewModel page
中添加模型并在View page
中调用@model
..
现在,在验证方法 controller 中调用 model 中存储的值。
VerifyProof(string proofRecordId)
方法调用另一个方法VerifyFirstName(proof)
来进行实际验证。
请看一下代码,你能指出在哪里添加model.HolderFirstName
和SendNameRequestViewModel model
在哪个方法中,例如VerifyProof(string proofRecordId)
, VerifyFirstName(proof)
.. 我得到一个错误。
[HttpGet]
public async Task<IActionResult> VerifyProof(string proofRecordId, SendNameRequestViewModel model)
{
var agentContext = await _agentProvider.GetContextAsync();
var proofRecord = await _proofService.GetAsync(agentContext, proofRecordId);
var request = JsonConvert.DeserializeObject<ProofRequest>(proofRecord.RequestJson);
var proof = JsonConvert.DeserializeObject<PartialProof>(proofRecord.ProofJson);
bool verified = false;
switch (request.Name)
{
case "ProveYourFirstName":
verified = VerifyFirstName(proof, model.HolderFirstName); break;
default:
break;
}
if (!verified)
{
proofRecord.State = ProofState.Rejected;
await _walletRecordService.UpdateAsync(agentContext.Wallet, proofRecord);
}
return RedirectToAction("Index");
}
public bool VerifyFirstName(PartialProof proof, SendNameRequestViewModel model.HolderFirstName)
{
var firstName = model.HolderFirstName;
var name = proof.RequestedProof.RevealedAttributes.First();
if (name.Value.Raw.Equals(firstName))
{
return true;
}
return false;
}
首先,controller class 中的操作/方法旨在处理来自客户端到服务器的请求。 它们不仅仅是 class 中的方法。
因此,我认为您需要从参数holderFirstName
中out
关键字。 或者更好的是,使用视图 model 在视图和 controller 之间传递:
public class SendNameRequestViewModel
{
[Required]
public string ConnectionId { get; set; }
[Required]
public string HolderFirstName { get; set; }
}
public class ProofController : Controller
{
public async Task<IActionResult> SendNameRequest(string connectionId)
{
// Initialize the view model if needed, i.e., filling its ConnectionId either
// from query string or cache. I don't know how you get the connectionId
var agentContext = await _agentProvider.GetContextAsync();
var connectionRecord = await _walletRecordService.GetAsync<ConnectionRecord>(agentContext.Wallet, connectionId);
if (connectionRecord == null)
{
return NotFound();
}
var vm = new SendNameRequestViewModel
{
ConnectionId = connectionId
};
return View(vm);
}
}
然后在视图上,您将其 model 声明为SendNameRequestViewModel
这样您就不必对输入名称进行硬编码/
注意:我还为输入添加了验证摘要和验证消息。
@model SendNameRequestViewModel
...
<form class="input-group mb-3" method="post" asp-controller="Proof" asp-action="SendNameRequest">
<input type="hidden" asp-for="ConnectionId" />
<div asp-validation-summary="ModelOnly"></div>
<input asp-for="HolderFirstName" autocomplete="off" class="form-control"
placeholder="Enter First Name" aria-label="First Name" aria-describedby="basic-addon2">
<span asp-validation-for="HolderFirstName" class="text-danger"></span>
<div class="input-group-append">
<button class="btn btn-outline-info" type="submit">Request a Proof of First Name</button>
</div>
</form>
对于您的VerifyFirstName
检查,有很多方法可以做到这一点。 例如,您可以直接在 controller 操作体中运行其逻辑。 我将针对PartialProof
object 创建一个扩展方法:
public static class PartialProofExtensions
{
public static bool VerifyFirstName(this PartialProof proof, string firstName)
{
if (proof == null)
{
return false;
}
var name = proof.RequestedProof.RevealedAttributes
.FirstOrDefault();
return (name != null && name.Value.Raw.Equals(firstName));
}
}
当表单回发时,您可以在 action 方法中运行验证检查:
[HttpPost]
[ValidateAntiforgeryToken]
public async Task<IActionResult> SendNameRequest(SendNameRequestViewModel model)
{
if (ModelState.IsValid)
{
var agentContext = await _agentProvider.GetContextAsync();
var connectionRecord = await _walletRecordService.GetAsync<ConnectionRecord>(agentContext.Wallet, model.ConnectionId);
if (connectionRecord == null)
{
ModelState.AddModalError("", "Invalid connection Id.");
return View(model);
}
var proofNameRequest = await CreateProofNameMessage(connectionRecord);
if (!proofNameRequest.VerifyFirstName(model.HolderFirstName))
{
ModelState.AddModalError(nameof(model.HolderFirstName), "Invalid first name.");
return View(model);
}
await _messageService.SendAsync(agentContext.Wallet, proofNameRequest, connectionRecord);
return RedirectToAction("Index");
}
return View(model);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.