gpt4 book ai didi

asp.net-mvc - 如何确定 View 是用于 ASP.NET MVC 中的 GET 还是 POST?

转载 作者:行者123 更新时间:2023-12-03 11:35:06 24 4
gpt4 key购买 nike

MVC 使用 action 属性为 http get 或 post 映射相同的 View :

 [HttpGet] 
public ActionResult Index()
{
ViewBag.Message = "Message";
return View();
}

[HttpPost]
public ActionResult Index(decimal a, decimal b, string operation)
{
ViewBag.Message = "Calculation Result:";
ViewBag.Result = Calculation.Execute(a, b, operation);
return View();
}

在 MVC View 中,如何确定该 View 是用于 http get 还是 http post?

在 View 中是 IsPost
@{
var Message="";
if(IsPost)
{
Message ="This is from the postback";
}
else
{
Message="This is without postback";
}
}

PS:对于点网核心,它是:
Context.Request.Method == "POST"

最佳答案

System.Web.HttpContext.Current.Request.HttpMethod存储当前方法。或者只是 Request.HttpMethod在 View 内部,但如果您需要检查这一点,则您的方法可能有问题。

考虑使用 Post-Redirect-Get 模式来形成重新发布。

关于asp.net-mvc - 如何确定 View 是用于 ASP.NET MVC 中的 GET 还是 POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4994707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com