Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads
 
Add your own
alternative version

Streaming Chart Images as FileResult from MVC Controllers

, 10 Mar 2009 CPOL
Using a proper MVC pattern to stream an image from Microsoft Chart controls for the Microsoft .NET Framework 3.5.
Mvc_Chart.zip
Mvc_Chart
_svn
dir-prop-base
entries
format
prop-base
props
text-base
Default.aspx.cs.svn-base
Default.aspx.svn-base
Global.asax.cs.svn-base
Global.asax.svn-base
Mvc_Chart.csproj.svn-base
Web.config.svn-base
tmp
prop-base
props
text-base
App_Data
_svn
entries
format
prop-base
props
text-base
tmp
prop-base
props
text-base
bin
Mvc_Chart.dll
Content
_svn
entries
format
prop-base
props
text-base
Site.css.svn-base
tmp
prop-base
props
text-base
Controllers
_svn
entries
format
prop-base
props
text-base
AccountController.cs.svn-base
FileResult.cs.svn-base
HomeController.cs.svn-base
tmp
prop-base
props
text-base
Global.asax
Models
_svn
entries
format
prop-base
props
text-base
tmp
prop-base
props
text-base
Mvc_Chart.csproj.user
Properties
_svn
entries
format
prop-base
props
text-base
AssemblyInfo.cs.svn-base
tmp
prop-base
props
text-base
Scripts
_svn
entries
format
prop-base
props
text-base
jquery-1.2.6.js.svn-base
jquery-1.2.6.min.js.svn-base
MicrosoftAjax.debug.js.svn-base
MicrosoftAjax.js.svn-base
MicrosoftMvcAjax.debug.js.svn-base
MicrosoftMvcAjax.js.svn-base
tmp
prop-base
props
text-base
Views
_svn
entries
format
prop-base
props
text-base
Web.config.svn-base
tmp
prop-base
props
text-base
Account
_svn
entries
format
prop-base
props
text-base
ChangePassword.aspx.cs.svn-base
ChangePassword.aspx.designer.cs.svn-base
ChangePassword.aspx.svn-base
ChangePasswordSuccess.aspx.cs.svn-base
ChangePasswordSuccess.aspx.designer.cs.svn-base
ChangePasswordSuccess.aspx.svn-base
Login.aspx.cs.svn-base
Login.aspx.designer.cs.svn-base
Login.aspx.svn-base
Register.aspx.cs.svn-base
Register.aspx.designer.cs.svn-base
Register.aspx.svn-base
tmp
prop-base
props
text-base
Home
_svn
entries
format
prop-base
props
text-base
About.aspx.cs.svn-base
About.aspx.designer.cs.svn-base
About.aspx.svn-base
ControllerStream.aspx.cs.svn-base
ControllerStream.aspx.designer.cs.svn-base
ControllerStream.aspx.svn-base
Index.aspx.cs.svn-base
Index.aspx.designer.cs.svn-base
Index.aspx.svn-base
XamlAndCodeBehind.aspx.cs.svn-base
XamlAndCodeBehind.aspx.designer.cs.svn-base
XamlAndCodeBehind.aspx.svn-base
XamlOnly.aspx.cs.svn-base
XamlOnly.aspx.designer.cs.svn-base
XamlOnly.aspx.svn-base
tmp
prop-base
props
text-base
Shared
_svn
entries
format
prop-base
props
text-base
Error.aspx.cs.svn-base
Error.aspx.designer.cs.svn-base
Error.aspx.svn-base
LoginUserControl.ascx.cs.svn-base
LoginUserControl.ascx.designer.cs.svn-base
LoginUserControl.ascx.svn-base
Site.Master.cs.svn-base
Site.Master.designer.cs.svn-base
Site.Master.svn-base
tmp
prop-base
props
text-base
Mvc_Chart1.zip
dir-prop-base
entries
format
Default.aspx.cs.svn-base
Default.aspx.svn-base
Global.asax.cs.svn-base
Global.asax.svn-base
Mvc_Chart.csproj.svn-base
Web.config.svn-base
entries
format
Mvc_Chart.dll
entries
format
Site.css.svn-base
entries
format
AccountController.cs.svn-base
FileResult.cs.svn-base
HomeController.cs.svn-base
Global.asax
entries
format
Mvc_Chart.csproj.user
entries
format
AssemblyInfo.cs.svn-base
entries
format
jquery-1.2.6.js.svn-base
jquery-1.2.6.min.js.svn-base
MicrosoftAjax.debug.js.svn-base
MicrosoftAjax.js.svn-base
MicrosoftMvcAjax.debug.js.svn-base
MicrosoftMvcAjax.js.svn-base
entries
format
Web.config.svn-base
entries
format
ChangePassword.aspx.cs.svn-base
ChangePassword.aspx.designer.cs.svn-base
ChangePassword.aspx.svn-base
ChangePasswordSuccess.aspx.cs.svn-base
ChangePasswordSuccess.aspx.designer.cs.svn-base
ChangePasswordSuccess.aspx.svn-base
Login.aspx.cs.svn-base
Login.aspx.designer.cs.svn-base
Login.aspx.svn-base
Register.aspx.cs.svn-base
Register.aspx.designer.cs.svn-base
Register.aspx.svn-base
entries
format
About.aspx.cs.svn-base
About.aspx.designer.cs.svn-base
About.aspx.svn-base
ControllerStream.aspx.cs.svn-base
ControllerStream.aspx.designer.cs.svn-base
ControllerStream.aspx.svn-base
Index.aspx.cs.svn-base
Index.aspx.designer.cs.svn-base
Index.aspx.svn-base
XamlAndCodeBehind.aspx.cs.svn-base
XamlAndCodeBehind.aspx.designer.cs.svn-base
XamlAndCodeBehind.aspx.svn-base
XamlOnly.aspx.cs.svn-base
XamlOnly.aspx.designer.cs.svn-base
XamlOnly.aspx.svn-base
entries
format
Error.aspx.cs.svn-base
Error.aspx.designer.cs.svn-base
Error.aspx.svn-base
LoginUserControl.ascx.cs.svn-base
LoginUserControl.ascx.designer.cs.svn-base
LoginUserControl.ascx.svn-base
Site.Master.cs.svn-base
Site.Master.designer.cs.svn-base
Site.Master.svn-base
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;

namespace Mvc_Chart.Controllers
{

    [HandleError]
    [OutputCache(Location = OutputCacheLocation.None)]
    public class AccountController : Controller
    {

        // This constructor is used by the MVC framework to instantiate the controller using
        // the default forms authentication and membership providers.

        public AccountController()
            : this(null, null)
        {
        }

        // This constructor is not used by the MVC framework but is instead provided for ease
        // of unit testing this type. See the comments at the end of this file for more
        // information.

        public AccountController(IFormsAuthentication formsAuth, MembershipProvider provider)
        {
            FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
            Provider = provider ?? Membership.Provider;
        }

        public IFormsAuthentication FormsAuth
        {
            get;
            private set;
        }

        public MembershipProvider Provider
        {
            get;
            private set;
        }

        [Authorize]
        public ActionResult ChangePassword()
        {

            ViewData["Title"] = "Change Password";
            ViewData["PasswordLength"] = Provider.MinRequiredPasswordLength;

            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChangePassword(string currentPassword, string newPassword, string confirmPassword)
        {

            ViewData["Title"] = "Change Password";
            ViewData["PasswordLength"] = Provider.MinRequiredPasswordLength;

            // Basic parameter validation
            if (String.IsNullOrEmpty(currentPassword))
            {
                ModelState.AddModelError("currentPassword", "You must specify a current password.");
            }
            if (newPassword == null || newPassword.Length < Provider.MinRequiredPasswordLength)
            {
                ModelState.AddModelError("newPassword",
                    String.Format(CultureInfo.CurrentCulture,
                         "You must specify a new password of {0} or more characters.",
                         Provider.MinRequiredPasswordLength));
            }
            if (!String.Equals(newPassword, confirmPassword, StringComparison.Ordinal))
            {
                ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
            }

            if (ModelState.IsValid)
            {
                // Attempt to change password
                MembershipUser currentUser = Provider.GetUser(User.Identity.Name, true /* userIsOnline */);
                bool changeSuccessful = false;
                try
                {
                    changeSuccessful = currentUser.ChangePassword(currentPassword, newPassword);
                }
                catch
                {
                    // An exception is thrown if the new password does not meet the provider's requirements
                }

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

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

        public ActionResult ChangePasswordSuccess()
        {

            ViewData["Title"] = "Change Password";

            return View();
        }

        public ActionResult Login()
        {

            ViewData["Title"] = "Login";

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(string username, string password, bool rememberMe, string returnUrl)
        {

            ViewData["Title"] = "Login";

            // Basic parameter validation
            if (String.IsNullOrEmpty(username))
            {
                ModelState.AddModelError("username", "You must specify a username.");
            }
            if (String.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("password", "You must specify a password.");
            }

            if (ViewData.ModelState.IsValid)
            {
                // Attempt to login
                bool loginSuccessful = Provider.ValidateUser(username, password);

                if (loginSuccessful)
                {
                    FormsAuth.SetAuthCookie(username, rememberMe);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("_FORM", "The username or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["rememberMe"] = rememberMe;
            return View();
        }

        public ActionResult Logout()
        {

            FormsAuth.SignOut();

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

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity is WindowsIdentity)
            {
                throw new InvalidOperationException("Windows authentication is not supported.");
            }
        }

        public ActionResult Register()
        {

            ViewData["Title"] = "Register";
            ViewData["PasswordLength"] = Provider.MinRequiredPasswordLength;

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(string username, string email, string password, string confirmPassword)
        {

            ViewData["Title"] = "Register";
            ViewData["PasswordLength"] = Provider.MinRequiredPasswordLength;

            // Basic parameter validation
            if (String.IsNullOrEmpty(username))
            {
                ModelState.AddModelError("username", "You must specify a username.");
            }
            if (String.IsNullOrEmpty(email))
            {
                ModelState.AddModelError("email", "You must specify an email address.");
            }
            if (password == null || password.Length < Provider.MinRequiredPasswordLength)
            {
                ModelState.AddModelError("password",
                    String.Format(CultureInfo.CurrentCulture,
                         "You must specify a password of {0} or more characters.",
                         Provider.MinRequiredPasswordLength));
            }
            if (!String.Equals(password, confirmPassword, StringComparison.Ordinal))
            {
                ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
            }

            if (ViewData.ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                MembershipUser newUser = Provider.CreateUser(username, password, email, null, null, true, null, out createStatus);

                if (newUser != null)
                {
                    FormsAuth.SetAuthCookie(username, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));
                }
            }

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

        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://msdn.microsoft.com/en-us/library/system.web.security.membershipcreatestatus.aspx for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "Username already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A username 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.";
            }
        }
    }

    // The FormsAuthentication type is sealed and contains static members, so it is difficult to
    // unit test code that calls its members. The interface and helper class below demonstrate
    // how to create an abstract wrapper around such a type in order to make the AccountController
    // code unit testable.

    public interface IFormsAuthentication
    {
        void SetAuthCookie(string userName, bool createPersistentCookie);
        void SignOut();
    }

    public class FormsAuthenticationWrapper : IFormsAuthentication
    {
        public void SetAuthCookie(string userName, bool createPersistentCookie)
        {
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }
}

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 Code Project Open License (CPOL)

Share

About the Author

Nic_Roche
Software Developer Encore Software
Australia Australia
Contractor in Desktop and Web applications.
Gold Coast, Queensland.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 10 Mar 2009
Article Copyright 2009 by Nic_Roche
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid