Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367 MVC 3 – Scattered Code

C# MVC Drop down List for Months

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367
public IEnumerable Months
{
    get 
    {
        return DateTimeFormatInfo
               .InvariantInfo
               .MonthNames
               .Select((monthName, index) => new SelectListItem
               {
                   Value = (index + 1).ToString(),
                   Text = monthName
               });
    }
}

Background tasks in MVC and IIS

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

As you might've noticed, keeping threads running after a request returns, for processing post operational tasks (such as performing analytics on a file that was uploaded, etc) don't always complete in a web project. There are several issues with spawning threads in the context of an ASP.NET project. Phil Haack's post explains the issues in more detail. The following classes solve the problem of IIS killing threads before they complete. First part is the IISTaskManager: using NLog; using System; using System.Threading.Tasks; using System.Web.Hosting; namespace Web.Models { /// <summary> /// Static...

Localization in c#

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Localization in C# is very straightforward. C# comes with Resource files, which are great because they can be easily translated, and C# can switch between them automatically.   First, add a new Resource File to your project. In my case, I called it SiteFields.resx. This will be the default language file, which in this case is English.   Next, we create a Localized Display Name Attribute, so we can leverage our ViewModels properly, instead of relying on text. public class LocalizedDisplayNameAttribute : DisplayNameAttribute { public LocalizedDisplayNameAttribute(string resourceId) : base(GetMessageFromResource(resourceId)) {...

Domain Routes and using subdomains in MVC as routes

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

I came across this when trying to build a website that takes advantage of sub domains in a MVC project. You can route subdomains in MVC to a particular controller, and specific actions.   In order to do this, you must make your website listen directly on the IP Address, and point a wildcard A record for all subdomains. In IIS 7, under Bindings, add a binding for http or https, set the IP to the dedicated IP for the site, and leave the Host name empty.   In your...

C# Thumbnail

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

In this tutorial I'll share how I create a c# thumbnail for each photo on our websites, and keep track of how many different sizes I need to use, while keeping images small for efficient load time, and efficient for server load. I will attach the Thumbnailer class at the end of the post. The thumbnailer can handle fixed width & height risize/crop, proportional resizing, width & height constraint resizing. The Thumbnailer can be used directly as an IHttpHandler or as a helper class for for another handler in your...

MVC Execute code on each controller method

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

The "OnActionExecuting" method is fired before every Action in a MVC controller. You can use this method to set and retrieve variables that are useful throughout your session, such as the person's name, their profile photo, etc. The following is an example implementation of this method public class HomeController : Controller { protected override void OnActionExecuting(ActionExecutingContext ctx) { base.OnActionExecuting(ctx); using (Repository rep = new Repository()) { if (Request.IsAuthenticated) { var profile = rep.GetProfileByID((Guid)Membership.GetUser().ProviderUserKey); ViewBag.Name = profile.DisplayName; } } } public ActionResult Index() { //your code return View(); } //rest of...

Create an XML Sitemap

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

XML sitemaps are accepted by search engines such as Google. Although it will not tell them to crawl pages included in the sitemap, it will tell them how to treat the pages they otherwise crawl, which can be very important in making the search engine understand your website. We start off with a Sitemap class using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Web.Mvc; using System.Web.Security; using DataAnnotationsExtensions; using System.Xml.Serialization; using System.Collections; namespace Site.Models { [XmlRoot("urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")] public class Sitemap { private ArrayList map; public Sitemap() {...

Create an RSS feed from your site

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

RSS is something quite standard, and as such, is easy to read and create. We start off with a FeedResult class, which inherits from ActionResult using System; using System.ServiceModel.Syndication; using System.Text; using System.Web; using System.Web.Mvc; using System.Xml; namespace Data.Controllers { public class FeedResult : ActionResult { public Encoding ContentEncoding { get; set; } public string ContentType { get; set; } private readonly SyndicationFeedFormatter feed; public SyndicationFeedFormatter Feed{ get { return feed; } } public FeedResult(SyndicationFeedFormatter feed) { this.feed = feed; } public override void ExecuteResult(ControllerContext context) { if (context ==...

Get a string description from an enum

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Enums can be very powerful when used appropriately, such as for populating fixed drop-down lists which almost never change   Let's say you want to save a person's gender in your database. You could save it as a string, but that's very wasteful. Ideally you'd save it as tinyint, because it will never have more values, so why waste space and computing time with joins, or other solutions. Your enum could look like this using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel; namespace Data.Enums { public enum Genders...

Log and email errors in MVC 3

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Logging of errors and tracing bugs is a very important part of development, so we've put together a script which records all relevant information at the point of the error, and logs it to our Error Tracking System, as well as sends out an email to the site administrator with the error. First, we want to record the website address where this error occurred, so we get this in 2 parts from ErrorPath = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.Path; RawUrl = Request.RawUrl;   Next, we get the last exception from...