MSTest on build-server

Keept getting errormessages like the following

C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1819,5): 
warning MSB3245: 
Could not resolve this reference. 
Could not locate the assembly "Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL". 
Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

Turns out there is a simpler solution than installing Visual Studio on the build-server.
there is a nuget package that resolves this error

PM> Install-Package Microsoft.VisualStudio.QualityTools.UnitTestFramework.Updated

ASP.net Sending mail using gmail

In order to send mail using a gmail address this can be used:

In Web.config

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

And then in the code (in this case C# is used)

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true; //reads the settings from web.config
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message
smtpClient.Send(mailMessage);

Or if you don’t wish to save any settings in web.config

string host = "smtp.gmail.com";
int port = 587;
SmtpClient smtpClient = new SmtpClient(host, port);
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message contained in mailMessage
smtpClient.Send(mailMessage);

SmtClient at msdn
It might also be interesting to read up on mailmessage

C# parsing through Outlook Calendar items

This code sample will simply parse through all Calendar Items and let us do something with them.
Note: Be aware of what is done here, if there are Items in the Calender with infinite reccurrences (no end date) then this will take forever to parse, so make sure something in the loop causes a break at some point.

	//using the Microsoft Outlook 12 Object library (Microsoft.Office.Interop.Outlook)
	using Microsoft.Office.Interop.Outlook
 
	Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
	Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace= oApp.GetNamespace("MAPI"); ;
	Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder= mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
	Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = CalendarFolder.Items;
 
	outlookCalendarItems.Sort("Start");
	//include recurring events (just a "normal" events i.e once per time it occurs)
	//Have to Sort by Start before setting IncludeRecurrences.
	outlookCalendarItems.IncludeRecurrences = true;
	foreach (Outlook.AppointmentItem item in outlookCalendarItems)
	{
		//Do something with the item
	}

Adding htmlattributes to a ActionLink

To add htmlattributes to a ActionLink might be needed for instance for the layout of the page.

@Html.ActionLink("Link text", "action", "Controller", null, new {style="float:left"})
//Adding a css class (need the @sign as class is a keyword for the compiler). It is also possible to use a Capitol C in Class but then it fails some validations.
@Html.ActionLink("Link text", "action", "Controller", null, new {@class="myCssClassName"})
 
//the syntax for the Html.ActionLink (used in this example is)
public static MvcHtmlString ActionLink(
	string linkText,
	string actionName,
	string controllerName,
	RouteValueDictionary routeValues,
	IDictionary<string, Object> htmlAttributes
)

C# MVC# create a URI from a Url.Action

Sometimes the need arises to create a URI object instead of just a link. This can be done by using Url.Action.

new Uri(Url.Action("action", "controller", null, Request.Url.Scheme))
 
//definitions 
public Uri(
	string uriString
)
 
public string Action(
	string actionName,
	string controllerName,
	Object routeValues,
	string protocol
)

Specifying what area an ActionLink should use

Using routevalues you can tell the html helper what area you want to create a action link to.

//create a link that will go to AreaName
Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{})
//Default area (= no area)
Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "" }, new{})
 
//create a link that will go to AreaName and with some htmlAttributes
Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{@class="myCssClass"})
 
 
//Definition
public static MvcHtmlString ActionLink(
	string linkText,
	string actionName,
	string controllerName,
	RouteValueDictionary routeValues,
	IDictionary<string, Object> htmlAttributes
)

asp.net C# Url.Action with routevalues

Using routevales you can change area and/or pass parameters on to the link you wish to hit.

//"normal" Url.Action - creates a link in the area where it is called from (and don't pass any values along)
<a href="@Url.Action("action", "controller")" class="myCssClass">link text</a>
//Url.Action with a that links to another area (area name or empty for the default area); also sets the category to shoes and it passes along the id that this view has.
<a href="@Url.Action("action", "controller", new { Area = "", id = Model.id, category="shoes"})" class="myCssClass">link text</a>

MVC3 Getting the URI in a controller

If there is a need to get the URI that the was used to reach the controller then the Request holds that information and it can be retrieved to a URI object

public ActionResult MyAction()
{
    var uri = Request.Url;
    var port = uri.Port;
    var absoluteUri = uri.AbsoluteUri
 
    //... do things here
 
    return View();    
}