加载资源失败:服务器响应状态为500(内部服务器错误)asp.net

时间:2018-09-22 04:29:40

标签: c# asp.net-mvc

这是我的控制者:

namespace Login.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Autherize(Login.Models.user usermodel)
        {

            using (LogEntities db = new LogEntities())
            {
                var userdetails = db.users.Where(x => x.USERNAME == usermodel.USERNAME && x.PASSWORD == usermodel.PASSWORD);
                if (userdetails == null)
                {
                    usermodel.ErrorMessage = "wrong inputs";
                    return View("Index", usermodel);
                }
                return View();
            }

        }
    }
}`

这是我的观点

    

    @using (Html.BeginForm("Autherize", "Login", FormMethod.Post))
    {
        <table>
            <tr>
                <td></td>
                <td> Login Form   </td>
            </tr>
            <tr>
                <td> @Html.LabelFor(model => model.USERNAME)   </td>
                <td> @Html.EditorFor(model => model.USERNAME)  </td>
            </tr>
            <tr>
                <td> @Html.ValidationMessageFor(model => model.USERNAME)   </td>
            </tr>
            <tr>
                <td> @Html.LabelFor(model => model.PASSWORD)   </td>
                <td> @Html.EditorFor(model => model.PASSWORD)  </td>
            </tr>
            <tr>
                <td> @Html.ValidationMessageFor(model => model.PASSWORD)   </td>
            </tr>
            <tr>
                <td colspan="2"> <label class="field-validation-error">@Html.DisplayFor(model => model.ErrorMessage)</label> </td>
            </tr>
            <tr>
                <td></td>
                <td> <input type="submit" name="login" value="login" /> </td>
            </tr>
        </table>
    }

</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

我遇到此错误。如何解决此错误?

  

找不到视图“授权”或其主视图,或者没有视图引擎   支持搜索的位置。以下位置是   搜索:

4 个答案:

答案 0 :(得分:0)

您已经在return View();方法中编写了Autherize,但是还没有在Autherize.cshtml文件夹中为Autherize(/Views/Login)创建视图页。

您可以创建Autherize.cshtml视图并将其返回,也可以重定向到已经存在的视图(可能是授权用户后希望将用户重定向到的页面)。

答案 1 :(得分:0)

您的Get方法名称为Index,因此您必须更改为Authorize,而不仅仅是它能够找到您的视图。

答案 2 :(得分:0)

return View();语句试图基于方法名称查找视图。从那以后,您没有提及任何视图名称。改为提及视图名称。

[HttpPost]
public ActionResult Autherize(Login.Models.user usermodel)
{
    using (LogEntities db = new LogEntities())
    {
        var userdetails = db.users.Where(x => x.USERNAME == usermodel.USERNAME 
            && x.PASSWORD == usermodel.PASSWORD);
        if (userdetails == null)
        {
            usermodel.ErrorMessage = "wrong inputs";
            return View("Index", usermodel);
        }
        return View('Autherize');
   }
}

答案 3 :(得分:0)

首先通过此

修改您的代码
data.frame

并用 public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(Login.Models.user usermodel) { using (LogEntities db = new LogEntities()) { var userdetails = db.users.Where(x => x.USERNAME == usermodel.USERNAME && x.PASSWORD == usermodel.PASSWORD); if (userdetails == null) { ViewData["ErrorMessage"] = "wrong inputs"; return View("Index"); } return View("Autherize"); } } 更改此@using (Html.BeginForm("Autherize", "Login", FormMethod.Post))

然后在@using (Html.BeginForm("Index", "Login", FormMethod.Post))

Index.cshtml

然后创建@if (ViewData["ErrorMessage"] != null) { //Write your error message here. <h5>User name and password wrong</h5> } 视图:

Autherize

如果您的USERNAME和PASSWORD不为null,请转到 public ActionResult Autherize() { return View(); } 视图。