mvc3 从视图重定向和从部分重定向都会导致 http 500 错误

发布于 2025-01-06 15:25:22 字数 2279 浏览 1 评论 0原文

当用户访问我的主页并单击登录按钮时,它会显示包含常规表单的 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>
        }

此表单存在于部分视图中,该视图是家庭控制器上索引视图的一部分。正如您所看到的,它发布到帐户控制器中的登录操作。下面就是这个动作。

    [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 页面上出现 500 错误。我已经检查了一百遍了,但我似乎无法弄清楚出了什么问题。数据是正确的。用户名和密码已验证,但我从未真正执行项目控制器上的索引操作。

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

我做了一些搜索,确实找到了一些关于从 ajax 等重定向的内容……还有一些内容说你不应该从部分重定向,但即使从常规视图来看它也不起作用。不确定是否是 IIS 问题或其他问题?绝对愿意尝试任何事情......为此奋斗了两天。

谢谢,

When a users visits my home page and they click the log in button it brings up jquery ui dialog that contains a regular form.

        @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>
        }

This form exists in a partial view that is part of the index view on the home controller. As you can see it posts to the Logon action in the Account controller. The following is that action.

    [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);
    }

As you can see if the model is not valid it sends you to the LogOn view (not a partial) which is part of the Account controller.

The problem is ... whether I fill out the form from the partial or the regular view either way I end up at the /Account/LogOn page with a 500 error. I've gone over it a hundred times and I can't seem to figure out what's going wrong. The data is correct. The user name and password are validated but I never actually make it to the Index action on the Project controller.

The index action in the Project controller exists and all it does is return View(). The Index.cshtml file does exist. Even if I remove all models and make the page as simple as possible I still end up with the same issue. Staying on the /Account/Logon page with a 500 error instead of making it to /Project/Index.

I have done some searching and did find some stuff about redirecting from ajax etc ... and something saying that you shouldn't redirect from a partial but even from the regular view it's not working. Not sure if it could be an IIS issue or something? Definitely willing to try anything ... been struggling on this for 2 days.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2025-01-13 15:25:22

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

@using(Html.BeginForm()){

; <字段集> <图例>帐户信息
        <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>

}

1) 500 Server error means that a script has thrown an error, this is not a broken link like 404 error.So use 'friendly http errors', this will give you a more comprehensive description of the error so you can debug the script.
2) Try using Razor syntax in Mvc 3 like below or using Strongly type on view.
3) May be your password property is wrong(it Password not password).

@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>

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文