【问题标题】:Server Error in '/' Application MVC5“/”应用程序 MVC5 中的服务器错误
【发布时间】:2016-04-30 04:33:03
【问题描述】:
 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();
            }
        }

索引 1 视图

@{
    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。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

请求的网址:/Calculator/Index1

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.34274

【问题讨论】:

    标签: asp.net-mvc-5


    【解决方案1】:
            // GET: Calculator
            [HttpGet] //  <---- instead of POST
            public ActionResult Index1(double a, double b, string op="+")
            {
            //....
            }
    

    【讨论】:

    • 我正在使用 [HttpGet] 浏览器通知“/”应用程序中的服务器错误。参数字典包含“MVCDemo.Controllers.CalculatorController”中方法“System.Web.Mvc.ActionResult Index1(Double, Double, System.String)”的不可为空类型“System.Double”的参数“a”的空条目'。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
    • Tks,使用 Get 代替 Post ^^
    • 请考虑提供更多信息,说明为什么这个答案会有所帮助
    【解决方案2】:

    检查你的默认路由,如果它指向Index1方法,那么它应该可以通过GET请求访问,而不是POST

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多