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 Scattered Code – Page 3 – Just another opsAI site

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

SQL Full Backup all databases

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

Backups are one of the most important tasks when maintaining a system, and as the complexity of your environment grows, you need tools to automate tasks. Using the SQL Agent, you can schedule the following stored procedure to back up every database and database log on your server (or modify it to your needs), excluding some stuff you can live without.   CREATE PROCEDURE BackupDatabases AS BEGIN     SET NOCOUNT ON;     DECLARE @name VARCHAR(50) /*database name*/     DECLARE @dbpath VARCHAR(256) /*path for backup db files*/     DECLARE @logpath VARCHAR(256)...

SQL Execute large scripts

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

SQL Server has a stored procedure that accepts a parameter of type text, and executes the code. This is useful if you need to run a large (over 8000 characters) dynamic query sys.sp_sqlexec

SQL Batch alter all columns

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

Sometimes you need to make mass changes to a database, and either add or remove a column, or other table or data manipulations. The script below prints SQL statements you can later execute for whatever you need. It loops through each table in your database, and replaces ? with the table name.   EXEC sp_msforeachtable 'PRINT ALTER TABLE ? ADD LastModified DATETIME NOT NULL DEFAULT GETDATE(); ALTER TABLE ? ADD UserID int NULL; ALTER TABLE ? ADD Deleted bit NOT NULL DEFAULT 0;' EXEC sp_msforeachtable 'PRINT Update ? SET SiteID=1'