Mvc DisplayAttribute Name based on resources

If you try to use resources in the property Name like this:

    public class ForgotViewModel
    {
        [Required]
        [Display(Name = Resources.Email)]
        public string Email { get; set; }
    }

You will get the error “An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type”

And this error is correct. So to use resources you will have to do something like this:

    public class ForgotViewModel
    {
        [Required]
        [Display(Name = "Email", ResourceType=typeof(Resources))]
        public string Email { get; set; }
    }

Now in your Resources type you will have to make sure that the Email property exists and that it’s Access Modifier is Public.

To may all Resources Public double click in your resx files and locate the drop box “Access Modifier” and change it’s value to Public.

 

Mvc5 EmailService implementation

This my generic EmailService Implementation. From here I can configure any email service like gmail.

public class EmailService : IIdentityMessageService
    {
        private const string ConfigurationEmailIsBodyHtml = "EmailIsBodyHtml";

        public static string From { get; set; }
        public static string UserName { get; set; }
        public static string Password { get; set; }
        public static string Host { get; set; }
        public static int Port { get; set; }
        public static bool EnableSsl { get; set; }
        public static bool IsBodyHtml { get; set; }
        public static bool UseDefaultCredentials { get; set; }
        public static SmtpDeliveryMethod DeliveryMethod { get; set; }

        static EmailService()
        {
            IsBodyHtml = ConfigurationManager.AppSettings[ConfigurationEmailIsBodyHtml].ToOrOther<bool>(true);

            SmtpSection section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

            if (section != null)
            {
                From = section.From;
                Host = section.Network.Host;
                Port = section.Network.Port;
                EnableSsl = section.Network.EnableSsl;
                UserName = section.Network.UserName;
                Password = section.Network.Password;
                DeliveryMethod = section.DeliveryMethod;
                UseDefaultCredentials = section.Network.DefaultCredentials;
            }

        }

        public Task SendAsync(IdentityMessage message)
        {

            var smtp = new SmtpClient
            {
                Host = Host,
                Port = Port,
                EnableSsl = EnableSsl,
                DeliveryMethod = DeliveryMethod,
                UseDefaultCredentials = UseDefaultCredentials,
                Credentials = new NetworkCredential(UserName, Password)
            };

            using (var smtpMessage = new MailMessage(From, message.Destination))
            {
                smtpMessage.Subject = message.Subject;
                smtpMessage.Body = message.Body;
                smtpMessage.IsBodyHtml = IsBodyHtml;
                return smtp.SendMailAsync(smtpMessage);
            }

            // Plug in your email service here to send an email.
            //return Task.FromResult(0);
        }
    }

Gmail configuration:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="username@gmail.com">
        <network host="smtp.gmail.com"
                 port="587"
                 enableSsl="true"
                 defaultCredentials="false"
                 userName="username@gmail.com"
                 password="password"/>
      </smtp>
    </mailSettings>
  </system.net>

 

Import CSV in MySql Workbench

This is a quick tip on how to import a csv with data into one of your tables.

1º Open MySql Workbench

2º run the following query

On linux:

load data local infile ‘/path/to/your/file.csv’ into table dbName.tableName fields terminated by ‘,’ enclosed by ‘”‘ lines terminated by ‘\n';

On Windows

load data local infile ‘c:\\path\\to\\your\\file.csv’ into table dbName.tableName fields terminated by ‘,’ enclosed by ‘”‘ lines terminated by ‘\n';

 

Download all Build 2014 videos

As you probably should know by now Build 2014 it’s over and it’s time to download the videos.

In this demo I will use MP4 high quality rss feed but it’s the same for other formats.


    class Program
    {
        public static XmlDocument getXMLDocumentFromXMLTemplate(string inURL)
        {
            HttpWebRequest myHttpWebRequest = null;     //Declare an HTTP-specific implementation of the WebRequest class.
            HttpWebResponse myHttpWebResponse = null;   //Declare an HTTP-specific implementation of the WebResponse class
            XmlDocument myXMLDocument = null;           //Declare XMLResponse document
            XmlTextReader myXMLReader = null;           //Declare XMLReader

            try
            {
                //Create Request
                myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(inURL);
                myHttpWebRequest.Method = "GET";
                myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

                //Get Response
                myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

                //Now load the XML Document
                myXMLDocument = new XmlDocument();

                //Load response stream into XMLReader
                myXMLReader = new XmlTextReader(myHttpWebResponse.GetResponseStream());
                myXMLDocument.Load(myXMLReader);
            }
            catch (Exception myException)
            {
                throw new Exception("Error Occurred in AuditAdapter.getXMLDocumentFromXMLTemplate()", myException);
            }
            finally
            {
                myHttpWebRequest = null;
                myHttpWebResponse = null;
                myXMLReader = null;
            }
            return myXMLDocument;
        }

        static void Main(string[] args)
        {
            XmlDocument doc = getXMLDocumentFromXMLTemplate("http://channel9.msdn.com/Events/Build/2014/RSS/mp4high");

            XmlNodeList list = doc.GetElementsByTagName("enclosure");
            StringBuilder sb = new StringBuilder();

            foreach(XmlNode node in list)
            {
                sb.AppendLine("wget " + node.Attributes["url"].Value);
            }

            string str = sb.ToString();
            //save the string to a file
        }
    }

The result will be a string like this:

 

wget http://media.ch9.ms/ch9/a2d5/f6d6b3fb-7b01-4bcb-96da-284d3622a2d5/C9Live9023_high.mp4
wget http://video.ch9.ms/sessions/build/2014/9-2663.mp4

….

just save that string to a cmd file and you are ready to download all the videos.

 

Load Jquery in Chrome developer console

If you for some reason need to load chrome in the developer console and that page doesn’t include Jquery this is what you will need to do:

For http page:

var scriptElem = document.createElement(‘script’);
scriptElem.src = “http://code.jquery.com/jquery.js”;
document.getElementsByTagName(‘head’)[0].appendChild(scriptElem);

 

For https page:

var scriptElem = document.createElement(‘script’);
scriptElem.src = “https://code.jquery.com/jquery.js”;
document.getElementsByTagName(‘head’)[0].appendChild(scriptElem);

 

Now you can start Jquery.

 

jquery-in-chrome-console