public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Zing", url: "zing/{type}/{p}", defaults: new { controller = "zing", action = "Index", type = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
Friday, December 06, 2013
Routing
When adding custom routing in MVC, any custom routing must go BEFORE the default. For example:
Tuesday, December 03, 2013
ToTitleCase method
The following lets you format a whole string in Title format; for example 'the news today' becomes 'The News Today' when applied.
string val = new CultureInfo("en-GB", false).TextInfo.ToTitleCase(val as String);
Wednesday, March 06, 2013
Code Prettifier
For a long time, I've been intending to make use of a good code "prettifier" - something which keeps the formatting of code when you blog it and that provides appropriate contrast on the page. I have discovered the "Blogger Code Prettifier" and have followed instructions to install it on my own blog. From what I can see so far, the results are good!
Check it out here
And here's what it can do!
static void Main(string[] args) { CopyObjectsUsingAutoMapper(); Console.ReadLine(); }
The joy of Auto-Mapper
Auto-Mapper solves a common problem - its mission is to make code that's used to map one object to another, obsolete. Its own website summarises the vision of the product adequately:
"AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?"
http://automapper.org/
I decided to throw together a really quick example.
First, I defined two identically built, but differently named, classes:
Then, in a separate class, I wrote some mapping logic:
I wanted to see the result of my mapping - to ensure it was working correctly - in a console window. To facilitate this, I decided to override the ToString() method in my AltPlanB class, like so:
My new AltPlanB class looked like this:
Then I wrote some simple calling code on the Console side:
Then I called this from my Main method:
Running the application produced the following output:
Going Further
Above is quite a straightforward example, but Auto-Mapper can go much further than that, for example objects do not necessarily have to be identical in order to map. By default, AM matches like for like (name and variable type) to do its mapping, but there are other possibilities too. See https://github.com/AutoMapper/AutoMapper/wiki to read up on what those possibilities are.
"AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?"
http://automapper.org/
I decided to throw together a really quick example.
First, I defined two identically built, but differently named, classes:
public class AltPlanA { public string Name { get; set; } public int FirstRowNumber { get; set; } public int LastRowNumber { get; set; } public string FirstRowLetter { get; set; } public string LastRowLetter { get; set; } } public class AltPlanB { public string Name { get; set; } public int FirstRowNumber { get; set; } public int LastRowNumber { get; set; } public string FirstRowLetter { get; set; } public string LastRowLetter { get; set; } }
Then, in a separate class, I wrote some mapping logic:
public class AutoMapEx { public AutoMapEx() { } public AltPlanB DoMapping() { // Create the map Mapper.CreateMap(); // Define source (this might, for example, come back from a DB) AltPlanA planA = new AltPlanA() { FirstRowLetter = "A", FirstRowNumber = 1, LastRowLetter = "Z", LastRowNumber = 10, Name = "My Plan" }; // Copy to another object AltPlanB planB = Mapper.Map (planA); return planB; } }
I wanted to see the result of my mapping - to ensure it was working correctly - in a console window. To facilitate this, I decided to override the ToString() method in my AltPlanB class, like so:
public override string ToString() { return "Name : " + Name + "\nFirstRowLetter: " + FirstRowLetter + "\nFirstRowNumber: " + FirstRowNumber.ToString() + "\nLastRowLetter: " + LastRowLetter + "\nLastRowNumber: " + LastRowNumber; }
My new AltPlanB class looked like this:
public class AltPlanB { public string Name { get; set; } public int FirstRowNumber { get; set; } public int LastRowNumber { get; set; } public string FirstRowLetter { get; set; } public string LastRowLetter { get; set; } public override string ToString() { return "Name : " + Name + "\nFirstRowLetter: " + FirstRowLetter + "\nFirstRowNumber: " + FirstRowNumber.ToString() + "\nLastRowLetter: " + LastRowLetter + "\nLastRowNumber: " + LastRowNumber; } }
Then I wrote some simple calling code on the Console side:
static void CopyObjectsUsingAutoMapper() { AutoMapEx mapper = new AutoMapEx(); AltPlanB bPlan = mapper.DoMapping(); Console.WriteLine(bPlan.ToString()); }
Then I called this from my Main method:
static void Main(string[] args) { CopyObjectsUsingAutoMapper(); Console.ReadLine(); }
Running the application produced the following output:
Going Further
Above is quite a straightforward example, but Auto-Mapper can go much further than that, for example objects do not necessarily have to be identical in order to map. By default, AM matches like for like (name and variable type) to do its mapping, but there are other possibilities too. See https://github.com/AutoMapper/AutoMapper/wiki to read up on what those possibilities are.
Monday, March 04, 2013
Accessing a TFS file that has not been checked in (by someone other than you)
In the scenario where a member of your team has checked out a file, disappeared from the company, thus rendering their profile/machine unusable, take the following steps to rectify the situation:
Replace 'OtherUserWorkspace' with the workspace name where the file is checked out (i.e. the user's computer name)
Replace 'OtherUser' with the username that has checked out the file
Replace '$/Project/ItemName.cs' with the full path (including all namespaces) with the file that needs to be undone
Example:
tf undo /workspace:WRK0130-PC;rbobby $/MISV2/www/Web.config.
You'll get an error such as the following when you first attempt to access the file:
- Open a Visual Studio command prompt
- Type the following: tf undo /workspace:OtherUserWorkSpace;OtherUser $/Project/ItemName.cs
Replace 'OtherUserWorkspace' with the workspace name where the file is checked out (i.e. the user's computer name)
Replace 'OtherUser' with the username that has checked out the file
Replace '$/Project/ItemName.cs' with the full path (including all namespaces) with the file that needs to be undone
Example:
tf undo /workspace:WRK0130-PC;rbobby $/MISV2/www/Web.config.
You'll get an error such as the following when you first attempt to access the file:
Subscribe to:
Posts (Atom)
Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.
Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.