Click here to Skip to main content
15,884,425 members
Articles / Web Development / ASP.NET

MVC Basic Site: Step 1 – Multilingual Site Skeleton

Rate me:
Please Sign up or sign in to vote.
4.90/5 (98 votes)
25 Oct 2013Ms-PL15 min read 406K   16.2K   319  
This article is intended to be the first one from this series and is focused mainly in the creation of a multilingual MVC web site skeleton.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Data;
using System.Data.Entity;
//
using MvcBasicSite.Models;
using MvcBasic.Logic;

namespace MvcBasicSite.Controllers
{
    /// <summary>
    /// Defines the account controller.
    /// </summary>
    public class AccountController : BaseController
    {
        

        /// <summary>
        /// Change the current culture.
        /// </summary>
        /// <param name="culture">The current selected culture.</param>
        /// <returns>The action result.</returns>
        public ActionResult ChangeCurrentCulture(int culture)
        {
            //
            // Change the current culture for this user.
            //
            SiteSession.CurrentUICulture = culture;
            //
            // Cache the new current culture into the user HTTP session. 
            //
            Session["CurrentUICulture"] = culture;
            //
            // Redirect to the same page from where the request was made! 
            //
            return Redirect(Request.UrlReferrer.ToString());
        }

        /// <summary>
        /// Activate Log-On page.
        /// </summary>
        /// <returns>The view.</returns>
        public ViewResult LogOn()
        {
            //
            // Create the model.
            //
            LogOnModel model = new LogOnModel();
            model.Username = string.Empty;
            model.Password = string.Empty;
            //
            // Activate the view.
            //
            return View(model);
        }

        /// <summary>
        /// Validate the user login data.
        /// </summary>
        /// <param name="model">The Log-On model.</param>
        /// <returns>The view result.</returns>
        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                //
                // Verify the user name and password.
                //
                User user = _db.Users.FirstOrDefault(item => item.Username.ToLower() == model.Username.ToLower() && item.Password == model.Password);
                if (user == null || user.Password != model.Password)
                {
                    ModelState.AddModelError("", Resources.Resource.LogOnErrorMessage);
                    //
                    return View(model);
                }
                else
                {
                    //
                    // User logined succesfully ==> create a new site session!
                    //
                    FormsAuthentication.SetAuthCookie(model.Username, false);
                    //
                    SiteSession siteSession = new SiteSession(_db, user);
                    Session["SiteSession"] = siteSession; // Cache the user login data!
                    //
                    return RedirectToAction("Index", "Home");
                }
            }
            //
            // If we got this far, something failed, redisplay form!
            //
            return View(model);
        }
        
        /// <summary>
        /// Log off.
        /// </summary>
        /// <returns>The action result.</returns>
        [Authorize]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            SiteSession siteSession = this.CurrentSiteSession;
            //
            // Clear the user session!
            //
            int culture = SiteSession.CurrentUICulture;
            this.Session["SiteSession"] = null;
            //
            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// Get the register page.
        /// </summary>
        /// <remarks>
        /// GET: /Account/Register
        /// </remarks>
        /// <returns>The action result.</returns>
        public ActionResult Register()
        {
            User registerUser = new User();
            registerUser.Address = new Address();
            //
            return View(registerUser);
        }

        /// <summary>
        /// Register the user data.
        /// </summary>
        /// <remarks>
        /// POST: /Account/Register
        /// </remarks>
        /// <param name="model">The model.</param>
        /// <returns>The view result.</returns>
        [HttpPost]
        public ViewResult Register(User model, Address modelAddress)
        {
            if (ModelState.IsValid)
            {
                //
                // Verify if exists other user with the same username.
                //
                User existUser = _db.Users.FirstOrDefault(item => item.Username.ToLower() == model.Username.ToLower());
                if (existUser == null)
                {
                    //
                    // Save the user data.
                    //
                    MvcBasic.Logic.User user = new MvcBasic.Logic.User();
                    user.Username = model.Username;
                    user.Password = model.Password;
                    user.UserRole = UserRoles.SimpleUser;
                    user.Email = model.Email;
                    //
                    if (modelAddress.CountryID <= 0)
                        modelAddress.CountryID = null;
                    //
                    user.Address = modelAddress;
                    //
                    _db.Users.AddObject(user);
                    _db.SaveChanges(); 
                    //
                    // Go to RegisterFinalized page!
                    //
                    return View("RegisterFinalized");
                }
                else
                {
                    //
                    // Exists other user with the same username, so show the error message.
                    //
                    ModelState.AddModelError("", Resources.Resource.RegisterInvalidUsername);
                    model.Address = modelAddress;
                    //
                    return View(model);
                }
            }
            //
            // If we got this far, something failed, redisplay form!
            //
            model.Address = modelAddress;
            //
            return View(model);
        }

        /// <summary>
        /// Cancel the user registration.
        /// </summary>
        /// <remarks>
        /// GET: /Account/CancelRegister
        /// </remarks>
        /// <returns>The action result.</returns>
        public ActionResult CancelRegister()
        {
            return RedirectToAction("LogOn");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Romania Romania
I have about 20 years experiences in leading software projects and teams and about 25 years of working experience in software development (SW Developer, SW Lead, SW Architect, SW PM, SW Manager/Group Leader).

Comments and Discussions