【问题标题】:mvc3 redirect from view and redirect from partial both cause http 500 errormvc3 从视图重定向和从部分重定向都导致 http 500 错误
【发布时间】:2012-02-17 06:41:58
【问题描述】:

当用户访问我的主页并单击登录按钮时,它会弹出包含常规表单的 jquery ui 对话框。

        @using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { @name = "user-login-form", @id = "user-login-form" }))
        {
            <fieldset style="margin: 10px 0 0 0;">
                <label for="name">
                    Username</label>
                <input type="text" name="UserName" id="username" class="text ui-widget-content ui-corner-all" />
                <label for="password">
                    Password</label>
                <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
            </fieldset>
        }

此表单存在于部分视图中,该部分视图是主控制器上索引视图的一部分。如您所见,它发布到 Account 控制器中的 Logon 操作。以下是那个动作。

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl) // rename either the model or the view name field to match.
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                AuthenticationService.SignIn(model.UserName, model.RememberMe);

                if (Url.IsLocalUrl(returnUrl))
                {
                    return Redirect(returnUrl);
                }

                if (MembershipService.IsUserInRole(model.UserName, "ServiceProvider"))
                {
                    return RedirectToAction("Index", "ServiceProvider");
                }

                return RedirectToAction("Index", "Project");
             }

            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

如您所见,如果模型无效,它会将您发送到作为 Account 控制器一部分的 LogOn 视图(不是部分视图)。

问题是......无论我是从部分视图还是常规视图填写表单,最终都会在 /Account/LogOn 页面出现 500 错误。我已经检查了一百次,但我似乎无法弄清楚出了什么问题。数据是正确的。用户名和密码已经过验证,但我从未真正进入项目控制器上的索引操作。

项目控制器中的索引操作存在,它所做的只是返回 View()。 Index.cshtml 文件确实存在。即使我删除了所有模型并使页面尽可能简单,我仍然会遇到同样的问题。停留在 /Account/Logon 页面,出现 500 错误,而不是进入 /Project/Index。

我已经进行了一些搜索,并确实找到了一些关于从 ajax 等重定向的东西......还有一些东西说你不应该从部分重定向,但即使从常规视图它也不起作用。不确定它是否可能是 IIS 问题或什么?绝对愿意尝试任何事情……为此苦苦挣扎了 2 天。

谢谢,

【问题讨论】:

    标签: asp.net-mvc-3 redirecttoaction


    【解决方案1】:

    1) 500 服务器错误表示脚本抛出了错误,这不是像 404 错误那样的断开链接。所以使用“友好的 http 错误”,这将为您提供更全面的错误描述,以便您进行调试剧本。 2) 尝试在 Mvc 3 中使用 Razor 语法,如下所示或在视图中使用强类型。 3)可能是您的密码属性错误(密码不是密码)。

    @using (Html.BeginForm()) {

    <div>
        <fieldset>
            <legend>Account Information</legend>
    
            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </div>
    
    
            <p>
                <input type="submit" value="Log On" />
            </p>
        </fieldset>
    </div>
    

    }

    【讨论】:

    • 你是对的......这是一个隐藏在目标视图中的编译错误。我试图通过说显然不存在的@html.Partial("...") 来呈现部分...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多