2012-04-15 173 views
1

这让我感到莫名其妙。它正在工作,现在不是。出于某种原因,我的AccountController Logon()方法不能再被找到,我不明白为什么。404寻找〜/帐户/登录

我的Global.asax.cs:

public static readonly ILog Log = LogManager.GetLogger(typeof(MvcApplication)); 

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new HandleErrorAttribute()); 
    } 

    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      null, 
      "{area}/{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters 
      new {area = string.Empty, controller = "Home", action="Paginate", searchString = UrlParameter.Optional } // Parameter defaults 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{area}/{controller}/{action}/{id}", // URL with parameters 
      new {area = string.Empty, controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 
    } 

    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 
     ControllerBuilder.Current.DefaultNamespaces.Add("C3.WebUI.Controllers"); 
    } 

我已经注册了2个区,AdminFinance

我的AccountController:

using System; 
using System.Web.Mvc; 
using System.Web.Security; 
using C3.WebUI.Models; 

namespace C3.WebUI.Controllers 
{ 
    public class AccountController : SecureController 
    { 

     // 
     // GET: /Account/LogOn 

     public ActionResult LogOn() 
     { 
      var model = new LogOnModel(); 
      return View(model); 
     } 

     // 
     // POST: /Account/LogOn 

     [HttpPost] 
     public ActionResult LogOn(LogOnModel model, string returnUrl) 
     { 
      if (ModelState.IsValid) 
      { 
       if (Membership.ValidateUser(model.UserName, model.Password)) 
       { 
        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
        Session["User"] = Membership.GetUser(model.UserName, true); 

        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
         && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
        { 
         return Redirect(returnUrl); 
        } 
        else 
        { 
         return RedirectToAction("Index", "Home"); 
        } 
       } 
       else 
       { 
        ModelState.AddModelError("", "The user name or password provided is incorrect."); 
       } 
      } 

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

     // 
     // GET: /Account/LogOff 

     public ActionResult LogOff() 
     { 
      FormsAuthentication.SignOut(); 
      Session.Abandon(); 

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

     // 
     // GET: /Account/Register 

     public ActionResult Register() 
     { 
      return View(); 
     } 

     // 
     // POST: /Account/Register 

     [HttpPost] 
     public ActionResult Register(RegisterModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       // Attempt to register the user 
       MembershipCreateStatus createStatus; 
       Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); 

       if (createStatus == MembershipCreateStatus.Success) 
       { 
        FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */); 
        return RedirectToAction("Index", "Home"); 
       } 
       else 
       { 
        ModelState.AddModelError("", ErrorCodeToString(createStatus)); 
       } 
      } 

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

     // 
     // GET: /Account/ChangePassword 

     [Authorize] 
     public ActionResult ChangePassword() 
     { 
      return View(); 
     } 

     // 
     // POST: /Account/ChangePassword 

     [Authorize] 
     [HttpPost] 
     public ActionResult ChangePassword(ChangePasswordModel model) 
     { 
      if (ModelState.IsValid) 
      { 

       // ChangePassword will throw an exception rather 
       // than return false in certain failure scenarios. 
       bool changePasswordSucceeded; 
       try 
       { 
        MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */); 
        changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword); 
       } 
       catch (Exception) 
       { 
        changePasswordSucceeded = false; 
       } 

       if (changePasswordSucceeded) 
       { 
        return RedirectToAction("ChangePasswordSuccess"); 
       } 
       else 
       { 
        ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); 
       } 
      } 

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

     // 
     // GET: /Account/ChangePasswordSuccess 

     public ActionResult ChangePasswordSuccess() 
     { 
      return View(); 
     } 

     #region Status Codes 
     private static string ErrorCodeToString(MembershipCreateStatus createStatus) 
     { 
      // See http://go.microsoft.com/fwlink/?LinkID=177550 for 
      // a full list of status codes. 
      switch (createStatus) 
      { 
       case MembershipCreateStatus.DuplicateUserName: 
        return "User name already exists. Please enter a different user name."; 

       case MembershipCreateStatus.DuplicateEmail: 
        return "A user name for that e-mail address already exists. Please enter a different e-mail address."; 

       case MembershipCreateStatus.InvalidPassword: 
        return "The password provided is invalid. Please enter a valid password value."; 

       case MembershipCreateStatus.InvalidEmail: 
        return "The e-mail address provided is invalid. Please check the value and try again."; 

       case MembershipCreateStatus.InvalidAnswer: 
        return "The password retrieval answer provided is invalid. Please check the value and try again."; 

       case MembershipCreateStatus.InvalidQuestion: 
        return "The password retrieval question provided is invalid. Please check the value and try again."; 

       case MembershipCreateStatus.InvalidUserName: 
        return "The user name provided is invalid. Please check the value and try again."; 

       case MembershipCreateStatus.ProviderError: 
        return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 

       case MembershipCreateStatus.UserRejected: 
        return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 

       default: 
        return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."; 
      } 
     } 
     #endregion 
    } 
} 

我已经改变很少在这里。

我〜查看/帐号/ LogOn.cshtml:

@model C3.WebUI.Models.LogOnModel 

@{ 
    ViewBag.Title = "Log On"; 
} 

<h2>Log On</h2> 
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") 

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

至于我可以告诉大家,一切看起来天经地义。为什么我在导航到/ Account/LogOn时收到404错误?ReturnUrl =%2f?

回答

3

好吧,没关系,我明白了。

我的路线注册,我想我应该指定区域作为路线的一部分;这显然是不正确的。一旦我删除了路由注册的area部分,一切都恢复正常。