Monday, January 24, 2011

Removing the mixed content warning when using Bing Maps API v7.0 over SSL

The other day I was building a store locator for a colleague of mine.  I the application up and running without any issues although when it came time to deploy to production I ran into a problem.  The production environment was being ran over SSL and since the link I used to get the Bing Maps API was not using the HTTPS version I would get a mixed content warning.

Luckily the API documentation from Microsoft recommended a different link.

Here is the one I was using prior to the SSL upgrade:

<script charset="UTF-8" type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>

Here is the link I changed it to:

<script charset="UTF-8" type="text/javascript" src="https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&ssl=1"></script>

Now the problem is that this still does not work.  As it turns out there is an error in Microsoft’s documentation here.  Here is the correct version:

<script charset="UTF-8" type="text/javascript" src="https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&s=1"></script>

Note that the query string parameter at the end of the URL should be s=1 not ssl=1.  Making this caused the rest of the scripts loaded by the Bing Maps API to be delivered over SSL.  This got rid of the mixed content warning.

Sunday, January 16, 2011

LINQ to SQL data context modifications

Many times while working with LINQ to SQL I find my self needing some sort of read only version of the data context.  Many operations run against the data context are primarily reading data anyways.  Due to this there is no need for LINQ to SQL to keep track of the state of the items it gets from the database.  Below is an example of the data context modification I use for my personal website.

public partial class DevDaveDataContext
{
    public static DevDaveDataContext ReadOnly
    {
        get
        {
            return new DevDaveDataContext() { ObjectTrackingEnabled = false };
        }
    }

    public static DevDaveDataContext Instance
    {
        get
        {
            return new DevDaveDataContext();
        }
    }

}

With the above code in place, here are a couple different ways I could access the data context.

using (var db = DevDaveDataContext.ReadOnly)
{
    ...
}

var db = DevDaveDataContext.Instance;
var db = DevDaveDataContext.ReadOnly;

Monday, January 10, 2011

Localization in ASP.NET MVC2 with Data Annotations

Let’s start by getting everyone on the same page.  Create a new Project | C# | Web | ASP.NET MVC 2 Web Application using the .NET Framework 4. With or without a test project, it’s up to you.  In this example we will setting up both English and French translation.  Adding more languages should be straight forward.

Setting up the Resource folder structure.

image

 

At the root of your MVC application create a Resources folder.  Inside this folder create a folder structure like the one to the left.

I like to try and keep all the resources organized so under the Models and Views folders I like to break things apart by controller.  Ultimately how you choose to store these items is up to you.  Changes to the folder structure will affect the namespaces.

Adding resource files and what you need to know.
Adding a resource file is the easy part.  Making the setting changes that are needed turns out to be a repetitive process that will get old real fast.  Right click on the Views | Home folder and select Add | New Item.  In the Add new item dialog type the word resource into the search installed templates textbox in the upper right hand corner. Select the Resource File template and type Index.resx.
image

Once the file has been added it should open up in edit mode.  Make the following changes:
image

Accessing the resource details is pretty straight forward after this part.  For example you could type the fully qualified name of a resource property like “applicationname.Resources.Views.Home.Index.PageTitle”. 

image

That namespace tends to be a little long so add the following to the properties of the resource file.

Add a custom tool namespace to the resource file “ViewRes.Home”.  You will then be able to access the values like “ViewRes.Home.Index.PageTitle”.

This process turns out to be one of the repetitive processes I was referencing above.  In order to limit the namespace length this modification must be made on each file for each language although its not required.

Also note that the second part of the entered namespace will need to change depending on what folder you are in for example you may change the namespace to “ViewRes.Account”.

Let’s go ahead and add the French file.  Do the same as above but make sure the name of the resource file is Index.fr.resx.  Don’t forget to change the Custom Tool Namespace value in the properties of the resource file. Since this would be the French file make sure the values in the resource file are French.  I like to use Google translate (http://translate.google.com/) although I strongly recommend having this translation done professionally.  Here is the translation:

Message = Bienvenue sur ASP.NET MVC! Exemple de localisation
PageTitle = Page d'accueil avec une localisation

Note I use the following naming structure for my resource files:  {page}.{culture}.resx.  The culture value can be the short or long version of the culture string for example all of these would work.

  • Index.fr.resx
  • Index.en-fr.resx
  • etc…

If you do not include a culture in the file like we did with Index.resx then it will be considered the default.

Tuesday, January 4, 2011

My MVC Application Structure 1.0

Below is the current structure I am using when creating new MVC applications.  Please note the below structure is still in a state of flux.  I am still in the process of trying new things so you may see more posts in regard to this topic.

image
  • Content Folder: I like having the CSS, Images and Scripts under one folder.  I feel it makes things so much easier.  The Downloads folder is used to store my Word and PDF version of my resume.

  • Controllers:  Nothing special here, just you standard folder with many controllers.

  • Infrastructure:  As you can see this is broke down into many different sections. Most of the sections are pretty obvious what they hold.  The Data folder is used to store the LINQ to SQL classes.  The Service folder is basically storing the middle tier of the application, these classes are called from the controllers.

  • Models:  Still not 100% sure on this but I break up the models by controller / use.  I find this to be considerably easier to maintain.

  • Views:  Nothing special here either.

Monday, January 3, 2011

2011 New Years Resolution

So this blog thing has been kicking my butt.  I never know what to write.  I have lots of ideas, even ones I write up but don’t publish.  So this years resolution as far as blogging is concerned is:

Publish as least 24 blog posts this year.

I have put together a list of some ideas for blog posts at least 12 things are on that list now so hopefully I should make it to the goal before the end of the year.  Maybe, just maybe this blog thing will become easier and I will be able to consistently blog about the technical things I run into and solve on a daily basis.