asp.net MVC 形式 POST 在 Localhost 中工作但发布后不工作

asp.net MVC form POST working in Localhost but not working after publishing

HomeController.cs:

 public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(LoginInfo login)
        {
            if (Request.Form["Submit"] != null)
            {
                string Username = login.Username;
                string Password = login.Password;

                ViewBag.Massage = "";

                if (Username == "Admin" && Password == "123")
                {
                    ViewBag.Massage = "Login Successfull";

                    return RedirectToAction("MSISDN","UnSub");
                }
                else
                {
                    ViewBag.Massage = "Please Enter Valid Login Information";                    
                }
            }
            return View();
        }

Views/Home/Index.cshtml

<form name="myForm" action="/Home/Index"  method="post">
    <div align="center">
        <div style="color:red">@ViewBag.Massage</div>
        <br>
        <label for="Username">Username:</label>
        <input type="text" name="Username" id="Username">
        <label for="Password">Password:</label>
         <input type="password" name="Password" id="Password">
        <br>
        <input type="submit" name="Submit"value="Submit">

    </div>
</form>

此代码在我的本地主机上运行完美。但是当我将我的项目发布到 IIS 服务器时,它不起作用。它正在返回 -

Server Error: 404 File or Directory Not Found.

可能与我的物理路径和请求的路径冲突。这个问题怎么解决?? :(

form标签中的action的url可能有误:

  • 如果显示的url和post的数据相同,可以去掉action="/Home/Index"
  • 如果要指定url,请使用action="@Url.Action("Index")"
    • 它将自动解析 Url。

在操作前使用 tilde (~) 符号后,我的代码运行正常。 例如:

action="~/Home/Index"

使用这个标志后我的代码工作正常。减少了虚拟路径和物理路径之间的冲突。