Search

Sunday, January 26, 2014
0 comments

About the Folders in MVC Web Application in Visual Studio: Part 3

8:46 AMSunday, January 26, 2014
Controller folder represents the controller for the MVC application created, which have two controller by default i.e. AccountController and HomeController. The functions/methods defined in these classes are called Action results used to specify some rules for the view related to that action.

Every action have its related view responsible for handling user input. Every controller in MVC application have a suffix Controller as the name shown in the default controller folder. Programmer can also create its own controller by adding new from the right click on this folder.

The following action shows two type of login actions, first is for Get action and second is for Post action. When user request for login, get action requests, after submitting with required credentials it requests for post (HttpPost) action (the second one).

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

There should be a view named Login to make these actions effective. Like this action the AccountController also have Register action, logout action etc.

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top