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 C# – Page 2 – Scattered Code

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...

C# Active Directory

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

C# has very good integration with Active Directory, and you can query all the objects you need. First, you'll need to reference and import the following libraries: using System.DirectoryServices; using System.DirectoryServices.AccountManagement;   .Net has a UserPrincipal built-in class, which will allow you quick access to common objects such as the Name and User Principal name // create your domain context PrincipalContext ctx = new PrincipalContext(ContextType.Domain); // define a "query-by-example" principal - here, we search for a UserPrincipal // and with the first name (GivenName) of "Bruce" UserPrincipal qbeUser = new...

MVC Remote Validator

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

Let's take a look at a brand new MVC 3 validator - RemoteAttribute. The Remote validator is very simple to use and yet extremely powerful. Remote is for situations where it is impossible to duplicate server side validation logic on the client, often because the validation involves connecting to a database or calling a service. If all your other validation uses javascript and responds to the user's input immediately, then it is not a good user experience to require a post back to validate one particular field. This is where...

Operator Overloads 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

http://www.csharphelp.com/2006/03/c-operator-overloading/

Dynamic Linq for searching

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

  var predicate = PredicateBuilder.True(); var q = db.Profiles.Where(t => !t.aspnet_Users.aspnet_Membership.IsLockedOut); foreach (string keyword in pars) { string temp = keyword; predicate = predicate.And(t => t.FirstName.StartsWith(temp) || t.LastName.StartsWith(temp)); }

C# Render Razor View as String

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 Razor View Engine is very powerful, easy to use, and just overall great for binding data to HTML. But sometimes you need to render HTML for use in Emails, download pages, generate PDF's, or other purposes, so it would be neat if you could use the Razor View Engine to generate those for you, and just give you the HTML to do with it as you wish. Luckily we have this somewhat built-in, and here's a wrapper for it. public string RenderRazorViewToString(string viewName, object model) {   ViewData.Model = model;...