我正在尝试使路线正常工作,但是无论我做什么,我总是被发送回登录表单。 但是,它确实会切换到配置文件视图。但没有其他任何看法。
动作
[HttpPost]
public ActionResult Authorise(tblUser user)
{
using (TrinityEntities db = new TrinityEntities())
{
var userEmail = db.tblUsers.Where(x => x.Email == user.Email).FirstOrDefault();
var userPassword = db.tblUsers.Where(y => y.Password == user.Password).FirstOrDefault();
//Check if email registered
//if (userEmail == null)
//{
// ViewBag.LoginIncorrect = "E-mail not registered, do you want to register?";
// return View("Index", user);
//}
//check login correct
if (userEmail == null && userPassword == null)
{
ViewBag.LoginIncorrect = "E-mail or Password not correct";
return View("Index", user);
}
//Everything is correct so login
//else if(userEmail.ToString() == user.Email && userPassword.ToString() == user.Password)
//{
// Session["User ID"] = user.Id;
// return RedirectToAction("Index", "Home");
//}
else
{
//Session["UserID"] = user.Id;
////int userID = user.Id;
//if (user.BIO == null )//|| user.Preffered == null || user.Sex == null || user.BIO == null)
//{
// return RedirectToAction("index", "Chat");
//}
//return RedirectToAction("Index", "Home");
return Redirect("/chat");
}
}
}
登录视图
@model Trinity.Models.tblUser
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm("Authorise", "Auth", FormMethod.Post))
{
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<label class="label-warning">@ViewBag.LoginIncorrect</label>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
聊天视图
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>pChat — Private Chatroom</title>
<link rel="stylesheet" href="@Url.Content("~/Content/app.css")">
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">pChat - @ViewBag.currentUser.name </a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Log Out</a></li>
</ul>
</div>
</nav>
<!-- / Navigation Bar -->
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-3">
<aside class="main visible-md visible-lg">
<div class="row">
<div class="col-xs-12">
<div class="panel panel-default users__bar">
<div class="panel-heading users__heading">
Contacts (@ViewBag.allUsers.Count)
</div>
<div class="__no__chat__">
<p>Select a contact to chat with</p>
</div>
<div class="panel-body users__body">
<ul id="contacts" class="list-group">
@foreach (var user in @ViewBag.allUsers)
{
<a class="user__item contact-@user.id" href="#" data-contact-id="@user.id" data-contact-name="@user.name">
<li>
<div class="avatar">
<img src="@Url.Content("~/Content/no_avatar.png")">
</div>
<span>@user.name</span>
<div class="status-bar"></div>
</li>
</a>
}
</ul>
</div>
</div>
</div>
</div>
</aside>
</div>
<div class="col-xs-12 col-md-9 chat__body">
<div class="row">
<div class="col-xs-12">
<ul class="list-group chat__main"></ul>
</div>
<div class="chat__type__body">
<div class="chat__type">
<textarea id="msg_box" placeholder="Type your message"></textarea>
<button class="btn btn-primary" id="sendMessage">Send</button>
</div>
</div>
<div class="chat__typing">
<span id="typerDisplay"></span>
</div>
</div>
</div>
</div>
</div>
<script src="@Url.Content("~/Content/app.js")"></script>
</body>
</html>
<script>
etc....
和路由配置
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "Index", id = UrlParameter.Optional }
);
//routes.MapRoute(
// name: "Default",
// url: "",
// defaults: new { controller = "Auth", action = "Index", id = UrlParameter.Optional }
//);
routes.MapRoute(
name: "Profile",
url: "profile",
defaults: new { controller = "Profile", action = "Index" }
);
routes.MapRoute(
name: "Home",
url: "Home",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "ChatRoom",
url: "chat",
defaults: new { controller = "Chat", action = "index" }
);
routes.MapRoute(
name: "GetContactConversations",
url: "contact/conversations/{contact}",
defaults: new { controller = "Chat", action = "ConversationWithContact", contact = "" }
);
routes.MapRoute(
name: "PusherAuth",
url: "pusher/auth",
defaults: new { controller = "Auth", action = "AuthForChannel" }
);
routes.MapRoute(
name: "SendMessage",
url: "send_message",
defaults: new { controller = "Chat", action = "SendMessage" }
);
routes.MapRoute(
name: "MessageDelivered",
url: "message_delivered/{message_id}",
defaults: new { controller = "Chat", action = "MessageDelivered", message_id = "" }
);
}
}
我只希望它切换到其他视图,由于某种原因我无法上班。
答案 0 :(得分:0)
验证用户凭据后,您需要存储cookie:
else
{
//Session["UserID"] = user.Id;
////int userID = user.Id;
//if (user.BIO == null )//|| user.Preffered == null || user.Sex == null || user.BIO == null)
//{
// return RedirectToAction("index", "Chat");
//}
//return RedirectToAction("Index", "Home");
**FormsAuthentication.SetAuthCookie(username, false);**
return Redirect("/chat");
}