Skip to content

Instantly share code, notes, and snippets.

@chrisnicola
Created May 2, 2010 20:10
Show Gist options
  • Save chrisnicola/387399 to your computer and use it in GitHub Desktop.
Save chrisnicola/387399 to your computer and use it in GitHub Desktop.
Restful routning bug
//This is the route definition
public class Routes : RouteSet{
public Routes() {
Map("").To<HomeController>(x => x.Index());
Area<HomeController>("", () =>
Map("").To<HomeController>(x => x.Index()));
Map("rss").To<FeedController>(x => x.Rss());
Map("atom").To<FeedController>(x => x.Atom());
Resources<PostController>(
() => {
Member("id", HttpVerbs.Get);
Member("delete", HttpVerbs.Get);
});
Resources<TagController>(() => Only("show"));
Resource<LoginController>(() => {
Only("show");
Member("authenticate", HttpVerbs.Post);
Member("signout", HttpVerbs.Get);
});
Resources<UserController>();
Resource<BlogMLController>(() => {
Only("show");
Member("import", HttpVerbs.Post);
});
}
}
//This is a failing unit test:
[Test]
public void login_is_mapped_to_login_controller() {
"~/login".Route().ShouldMapTo<LoginController>(x => x.Show());
"~/login/signout".Route().ShouldMapTo<LoginController>(x => x.SignOut());
//This assertion fails, the rest pass
"~login/authenticate".Route().ShouldMapTo<LoginController>(x => x.Authenticate(null));
}
//This is the controller
public class LoginController : Controller{
readonly IUserTasks _userTasks;
public LoginController(IUserTasks userTasks) { _userTasks = userTasks; }
public ActionResult Show() {
if (_userTasks.IsLoggedIn()) return RedirectToAction("Index", "Home");
return View(new LoginViewModel());
}
[HttpPost, ValidateAntiForgeryToken, Transaction]
public ActionResult Authenticate(LoginViewModel model) {
_userTasks.AuthenticateUser(model.Username, model.Password);
return RedirectToAction("Index", "Home", new {area = "Admin"});
}
public ActionResult SignOut() {
_userTasks.SignOut();
return this.RedirectToAction<HomeController>(x => x.Index());
}
}
//This is the view
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="ModelViewPage<Graphite.Web.Controllers.Login.LoginViewModel>" %>
<%@ Import Namespace="Graphite.Web.Controllers.Login" %>
<asp:Content ContentPlaceHolderID="main" runat="server" >
<%
using (Html.BeginForm<LoginController>(x => x.Create(Model))) {%>
<div class="formInput">
<%= this.TextBox(x => x.Username).Label("Username: ")%>
</div>
<div class="formInput">
<%= this.Password(x => x.Password).Label("Password: ")%>
</div>
<%=Html.AntiForgeryToken()%>
<%=this.SubmitButton("Login")%>
<%
}%>
</asp:Content>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment