Monday, February 24, 2014

New features in MVC versions (MVC 3 vs. MVC 4 vs. MVC 5)

ASP.NET MVC 3
  • New Project Templates having support for HTML 5 and CSS 3.
  • Improved Model validation.
  • Razor View Engine introduced with a bundle of new features.
  • Having support for Multiple View Engines i.e. Web Forms view engine, Razor or open source.
  • Controller improvements like ViewBag property and ActionResults Types etc.
  • Unobtrusive JavaScript approach.
  • Improved Dependency Injection with new IDependencyResolver.
  • Partial page output caching.

ASP.NET MVC 4
  • ASP.NET Web API, a framework that simplifies the creation of HTTP services and serving a wide range of clients.
  • Adaptive rendering and other look-n-feel improvements to Default Project Templates.
  • A truly Empty Project Template.
  • Based on jQuery Mobile, new Mobile Project Template introduced.
  • Support for adding controller to other project folders also.
  • Task Support for Asynchronous Controllers.
  • Controlling Bundling and Minification through web.config.
  • Support for OAuth and OpenID logins using DotNetOpenAuth library.
  • Support for Windows Azure SDK 1.6 and new releases.

ASP.NET MVC 5
  • Bootstrap replaced the default MVC template.
  • ASP.NET Identity for authentication and identity management.
  • Authentication Filters for authenticating user by custom or third-party authentication provider.
  • With the help of Filter overrides, we can now override filters on a method or controller.
  • Attribute Routing is now integrated into MVC 5.

Source: http://www.webdevelopmenthelp.net/2014/02/ASP.NET-MVC3-Vs-MVC4-Vs-MVC5.html

Sunday, February 23, 2014

ASP.NET: Web Forms vs. MVC (how to decide)

Why ASP.NET Web Forms and Why ASP.NET MVC?

Each can be the “best choice” for a particular solution depending on the requirements of the application and the background of the team members involved. What to choose and when has more to do with business prospective than which one is better than other. When facing a decision to choose between ASP.NET Web Forms or ASP.NET MVC it is important to know that neither technology is meant to replace the other.

Two important factors you should consider while making the choice is:
  1. Rapid application development - If you want to develop anything rapidly ASP.NET Web Forms is the only chance you are having, you can’t even consider for ASP.NET MVC for RAD. (Reasons for RAD may be anything like client is not paying too much, or application is going to be used for only one or two months and won’t require much maintenance.)
  2. Unit Testing - If automatic unit testing is most important factor for you MVC will be best for you.
Other than these, what you can do is, write down all your project requirement and try to compare them with Pros and Cons of both Web Forms and MVC and if possible try to ask yourself following questions and point MVC and Web Forms accordingly
  1. Does your team have good experience with Web Forms or Windows Forms? Well, if yes then probably learning ASP.NET MVC is going to be a tedious task for team, because developers have been used to with ViewState and event driven programming by now and migration is going to be a difficult task.1 point to Web Forms.
  2. Does your team have good experience with ASP.NET MVC? If yes ASP.NET MVC get 1 point
  3. Does your team have experience on ASP or non-Microsoft technologies such as android, ios, JSP, ROR, PHP? If you have been JSP or ASP developer prior then you might be familiar with HTTP get and Post and even u might have hands on with MVC because most of them use MVC by default. It gives 1 point to ASP.NET MVC.
  4. Is JavaScript going to be used extensively? If Yes, MVC gets the point because you get complete control over HTML. 1 point ASP.NET MVC.
  5. Looking for good performance? With no support for ViewState ASP.NET MVC provides good performance gain over traditional ASP.NET Web Forms.1 point ASP.NET MVC.
  6. Planning to reuse the same input logic? If yes stick with MVC.
Source: http://www.codeproject.com/Articles/528117/WebForms-vs-MVC

Monday, December 23, 2013

Tortoise SVN Settings for Beyond Compare 4

I recently rebuilt my computer and had to re-enter my Tortoise SVN settings for Beyond Compare. Now I won't forget!

Diff Viewer
"C:\Program Files (x86)\Beyond Compare 4\BComp.exe" %base %mine /title1=%bname /title2=%yname /leftreadonly

Merge Tool
"C:\Program Files (x86)\Beyond Compare 4\BComp.exe" %mine %theirs %base %merged /title1=%yname /title2=%tname /title3=%bname /title4=%mname

Tuesday, October 15, 2013

Compress all JPG/JPEG files in a directory tree (simple console application)

I needed a quick application to increase the compression of all .jpg files in a directory tree. This application is a console program that takes three command-line parameters:
compressjpg inDir outDir compressionLevel extensions
compressLevel is a number from 0 to 100, where 0 is maximum compression and 100 is no compression. extensions is a comma/semi-colon delimited list of file extensions to process. For example:
C:> compressjpg c:\myfiles c:\newfiles 80 jpg;jpeg
Download the Visual Studio 2012, .NET 4.5 solution (source code) by clicking here.

Sunday, September 29, 2013

Load ASP.NET page routes from XML file using C#

Want to load your ASP.NET page routes from an XML file? Here is a sample file and a short snippet of code to load them. This supports not only basic page routes, but default items and route constraints.

Have fun!
<?xml version="1.0" encoding="utf-8" ?>
<pageRoute routeExisting="true">
   <routes>
      <!-- route with no defaults or constraints -->
      <route name="HomePage">
         <url>home</url>
         <file>~/default.aspx</file>
      </route>
      <!-- route with defaults -->
      <route name="FAQView">
         <url>faqs/{id}</url>
         <file>~/faqs/ViewFAQ.aspx</file>
         <defaults>
            <default key="id" value="1000" />
         </defaults>
      </route>
      <!-- route with constraints -->
      <route name="ProductView">
         <url>products/view/{id}</url>
         <file>~/products/ViewProduct.aspx</file>
         <constraints>
            <constraint key="id" value="\d+" />
         </constraints>
      </route>
   </routes>
</pageRoute>
Code:
private void RegisterRoutes(RouteCollection routes)
{
   routes.Ignore("{resource}.axd/{*pathInfo}"); 

   string path = Path.Combine(
      Server.MapPath("~/App_Data"),
      "PageRoute.xml");
   if (File.Exists(path))
   {
      // load file with all page routes
      XElement xmlPageRoutes = XElement.Load(path);

      // set some defaults
      routes.RouteExistingFiles = (bool)xmlPageRoutes.Attribute("routeExisting");

      // map each individual route
      foreach (XElement xmlRoute in xmlPageRoutes.XPathSelectElements("routes/route"))
      {
         // defaults
         RouteValueDictionary defaults = new RouteValueDictionary();
         foreach (XElement def in xmlRoute.XPathSelectElements("defaults/default"))
         {
            defaults.Add((string)def.Attribute("key"), (string)def.Attribute("value"));
         }

         //constraints
         RouteValueDictionary constraints = new RouteValueDictionary();
         foreach (XElement def in xmlRoute.XPathSelectElements("constraints/constraint"))
         {
            constraints.Add((string)def.Attribute("key"), (string)def.Attribute("value"));
         }

         routes.MapPageRoute(
            (string)xmlRoute.Attribute("name"),
            (string)xmlRoute.Element("url"),
            (string)xmlRoute.Element("file"),
            true,
            defaults,
            constraints);
      }
   }
}

Monday, August 19, 2013

Code Defensively

Code Defensively
  1. Use Microsoft's Code Contracts
  2. Don't call LINQ .FirstOrDefault without checking the return value
  3. Thow ApplicationException for internal checks (and check often)
  4. Check for null after casting a variable using "as"
  5. Think before you code  :-)

Sunday, June 9, 2013

IIS URL Rewrite non-www site to www

Use the IIS URL Rewrite module to route a site without "www" prefix to your standard "www" site.  Below is the web.config section to do this.
<system.webserver>
   <rewrite>
      <rules>
         <rule name="Redirect to WWW" stopprocessing="true">
            <match url="(.*)">
            <conditions>
               <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$">
               </add>
            </conditions>
            <action appendquerystring="true" type="Redirect" url="http://www.{HTTP_HOST}/{R:0}">
         </action></match></rule>
      </rules>
   </rewrite>
</system.webserver>

C# file touch utility and source code

At some point you need a simple, tiny, utility to change the dates on Windows files.  Attached is one I wrote, intentionally keeping it basic.  I've included a link to both the source code and just the .exe if you want that.  It does save settings across executions (except for specific date/time values).  Have fun.

6/25/2013: Updated to allow all times to sync together.

Monday, June 3, 2013

Parsing string to C# DateTime from specific time zone into UTC

If you need to parse a string representation of a DateTime into a specific time zone, use the DateTimeOffset in C# to assist.  For example, assume your code is executing in PST and you have a string like "1/6/2013 11:00:21 AM" that represents a time in EST.  How to do you convert this into UTC format?

Use the TimeZoneInfo.BaseUtcOffset member to create a DateTimeOffset structure.  Using this structure, you can then easily convert to UTC by by calling DateTimeOffset.UtcDateTime.


private void CalculateTimeToEastern()
{
  DateTime utc = ParseToTimeZone("1/6/2013 11:00:21 AM", "Eastern Standard Time").UtcDateTime;
}

private DateTimeOffset ParseToTimeZone(string s, string timeZoneName)
{
  TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName);
  DateTimeOffset offset = new DateTimeOffset(DateTime.Parse(s), easternZone.BaseUtcOffset);

  return offset;
}

Note from MSDN (which is interesting)
These uses for DateTimeOffset values are much more common than those for DateTime values. As a result, DateTimeOffset should be considered the default date and time type for application development.

Sunday, June 2, 2013

Capitalize each word in sentence using C#

For non-English cultures, change the TextInfo parameter.

public static string SentenceCase(string phrase)
{
   TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
   string capitalized = textInfo.ToTitleCase(phrase);

   return phrase;
}

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...