MVC actions, should I create 2 login actions?

In asp.net mvc, I want to create a login action.

So this is how I do it:

  • create an action / view login with a name that just displays the view.

  • create another action named login2 which will be the page processing the form post and will check the database if the username / password is correct. If so, redirect to some page, if not, redirect back to the login page with the appropriate error message.

Is this the best way to do it?

+1


source to share


3 answers


You can create two login actions to view and one to post the form. Then decorate them with the AcceptVerbs attribute to describe which method they will accept. See example here http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx



+5


source


Here's the template I'm using:



    /// <summary>
    /// Displays the Login screen the first time
    /// to anyone who wishes to view it.
    /// </summary>
    /// <returns></returns>
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Login()
    {
        return View();
    }

    /// <summary>
    /// Handles the form postback
    /// </summary>
    /// <returns></returns>
    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateAntiForgeryToken]
    public ActionResult Login(string name, 
                              string password, 
                              string ReturnUrl)
    {
        // perform authentication here

        if (string.IsNullOrEmpty(ReturnUrl))
            return RedirectToAction("Index", "Main");

        return Redirect(ReturnUrl);
    }

      

+1


source


I agree with Craig; however, if you want to do it in some other way, you must come up with some naming conventions to differentiate and adhere to your ActionMethods.

Before viewing 5, I used

login => authenticate Create => insert edit => update

and etc.

0


source







All Articles