About jontas

I like to make things that people find useful.

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();    
}

PHP Send a mail as UTF-8

To send a mail from PHP encoded as UTF-8 is fairly easy to do by modifying the headers.

//Sender with special chars (UTF-8) encoded
mail('mail@example.com', '=?UTF-8?B?'.base64_encode($subjectString).'?=', $messageString, "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n");
//Sender not UTF-8 encoded
mail('mail@example.com', 'sender@mail.com', $messageString, "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n");

Installing PHPUnit on ubuntu

The installation package that can be installed from apt includes a console tool to run PHPUnits, but is (currently) not the latest version. One way to get past this is to install PHPUnit from apt and then upgrade PHPUnit via PEAR note: (currently) PEAR needs to be updated as well.

user@devserver~: sudo apt-get install phpunit
user@devserver~: sudo pear channel-discover pear.phpunit.de
user@devserver~: sudo pear channel-discover components.ez.no
user@devserver~: sudo pear channel-discover pear.symfony-project.com
user@devserver~: sudo pear update
user@devserver~: sudo pear update phpunit/PHPUnit

The line that simply says “sudo pear update” should use updates per to the latest version. Also as the commenter says “upgrade” might be the right keyword to us for some versions.