【问题标题】:location.href not working on asp mvc [closed]location.href 不适用于asp mvc [关闭]
【发布时间】:2016-05-10 22:01:09
【问题描述】:

我有这个代码...

$("#Login").click(function()
 {
    var dataObject = { UserName: $("#UserName").val(), Password: $("#Password").val() };
    $.ajax({
        url: '@Url.Action("Login","User")',
        type: "POST",
        data: dataObject,
        datatype: "json",
        success: function (result) {
            if (result.toString() == "success") {
                alert(result);
                window.location.href = "~/Views/User/Home.cshtml";

            }
            else {
                alert(result);
                
            }
        },
        error : function(result)
        {
            alert("ERRORR")
        }
    });
})

我正在尝试重定向到主页...在成功警报之后...但它不起作用。我得到了:

“/”应用程序中的服务器错误。

不提供此类页面。

说明:您请求的页面类型未提供服务,因为它已被明确禁止。扩展名“.cshtml”可能不正确。请检查下面的 URL 并确保其拼写正确。

有什么想法吗?

这是我的用户控制器:

public class UserController : Controller
    {
        UserBusinessLogic UserBL = new UserBusinessLogic();
        //
        // GET: /User/
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(User User)
        {
            string message = "";
            if(ModelState.IsValid)
            {
                if(UserBL.CheckUserLogin(User) > 0)
                {
                    message="success";
                    
                }
                else
                {
                    message="Username or password not correct";
                }
            }
            else
            {
                message = "All Field Required";
            }
            if (Request.IsAjaxRequest())
            {
                return Json(message,JsonRequestBehavior.AllowGet);
            }
            else
            {
                return RedirectToAction("Index","Home");
            }
        }
    }

【问题讨论】:

    标签: javascript asp.net asp.net-mvc


    【解决方案1】:

    出于安全原因,Web 服务器默认不会为 *.cshtml 提供服务。

    我认为您想重定向到主页。如果是这样,它应该是这样的 -

    window.location.href = '@Url.Content("~")'; 
    

    或者

    window.location.href = '@Url.Content("~/User/Home")'; 
    

    或者

    window.location.href = '@Url.Action("Home","User")';
    

    【讨论】:

    • Home.cshtml 它是我创建的视图...我想在成功警报消息后重定向到它。我在任何控制器中都没有它。我只想做一个简单的重定向
    • Home Action 方法UserController 内吗?如果不是,它位于哪里?在尝试使您的代码工作之前,您应该能够在浏览器中浏览 URL。例如,http://localhost:xxx/user/home
    • 请检查我在我的问题中添加了 UserController 的代码,看看是否有什么好处,或者我应该添加一些东西以便我可以重定向到它。
    • @BoularbahHamza 您正在制作 Ajax 发布到操作方法,该方法重定向到另一个操作方法。首先,为什么要 Ajax 发布?
    • 我是初学者,我不知道用 asp mvc 发帖的其他方法。如果有任何方法可以做到。帮助
    【解决方案2】:

    我认为你对 MVC 模式有误解。

    “我想在成功警报消息后重定向到它。我不 将它放在任何控制器中。我只想做一个简单的重定向。”

    但这不是标准。通常,即使是最简单的请求,您也会创建一个控制器,即使它看起来像

    public class UserController : Controller
    {
        public ActionResult Home()
        {
            return View();
        }
    }
    

    然后定向到控制器的动作

    window.location.href = "/User/Home";
    

    【讨论】:

      【解决方案3】:

      您正在尝试重定向到 cshtml 视图。您需要重定向到控制器中的操作 - 大概是 /User/Home

      【讨论】:

      • 如何在控制器中重定向?
      • @BoularbahHamza 正如我提到的那样,通过将 href 更改为 /User/Home 或使用 Url.Action 帮助器。
      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 2013-08-05
      • 1970-01-01
      • 2014-09-25
      • 2017-05-30
      • 1970-01-01
      相关资源
      最近更新 更多