CalculatorController
public class CalculatorController : Controller
{
// GET: Calculator
[HttpPost]
public ActionResult Index1(double a, double b, string op="+")
{
switch(op)
{
case "+":
ViewBag.Ketqua = a + b;break;
case "-":
ViewBag.Ketqua = a + b;break;
case "*":
ViewBag.Ketqua = a + b;break;
case "/":
ViewBag.Ketqua = a + b;break;
}
return View();
}
}
Index1查看
@{
ViewBag.Title = "Index1";
}
<h2>Index1</h2>
<form name="formcalculator" action="/Calculator/Index1" method="post">
<div>
Nhập a: <input type="text" name="a" size="20" />
<br />
Nhập b: <input type="text" name="b" size="20" />
<br />
<input type="radio" name="op" value="+" /> +
<input type="radio" name="op" value="-" /> -
<input type="radio" name="op" value="*" /> *
<input type="radio" name="op" value="/" /> /
</div>
<div>
<input type="submit" value="Calculator" name="btnOK" />
@ViewBag.Ketqua
</div>
</form>
网址:http://localhost:1524/Calculator/Index1 但
Server Error in '/' Application.
无法找到资源。
描述:HTTP 404.您正在查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,确保拼写正确。
请求的网址:/ Calculator / Index1
版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.34274
答案 0 :(得分:0)
// GET: Calculator
[HttpGet] // <---- instead of POST
public ActionResult Index1(double a, double b, string op="+")
{
//....
}
答案 1 :(得分:0)
检查您的默认路由,如果它指向Index1
方法,那么它应该可以通过GET
请求访问,而不是POST