Tag Archives: .net

ASP.NET MVC 2 Localization and Local Resource Files

The complete guide to localization with Global Resources is at While my keyboard gently weeps – ASP.NET MVC 2 Localization complete guide but here’s a simple summary:

Using Global Resources for Asp.NET MVC Localization Quick Start

  1. Create ordinary resource files in your project and fill ’em up.
  2. Add a Page Import directive to your MVC view:
    <%@ Import namespace=”My.Project.Name.PathTo.WhereverYourResourceFileIs” %>
  3. Behold the compiled fields available to you in intellisense:
    <%=ResourceFileName.MyPropertyName %>

Using Local Resources for Asp.NET MVC Localization Quick Start

The net and his dog seems to want to discourage you from using Local resource files with MVC. There are two or three riddles and here’s how to solve them:

  1. Right click your view directory. choose Add -> Add Asp.Net Folder -> App_LocalResources.
  2. Create resource files in there with names to exactly match the View name (so you end up with MyView.aspx.resx as the file name).
  3. Change the access modifier from its default No Code Generation to Public
  4. Type in your resource strings
  5. Add a Page Import directive to your MVC view:
    <%@ Import namespace="My.Project.Name.Views.ViewName.App_LocalResources" %>

    . Get the exact namespace by opening up the Resource File’s aspx.Designer.cs file.

  6. While you’re in the resource Designer.cs file note the class name: ActionName_aspx
  7. Behold the compiled fields available to you in intellisense:
    <%=ActionName_aspx.MyPageSpecificPropertyName %>
  8. Be awed and depressed when you build and run and get the error message ”Could not find any resources appropriate for the specified culture or the neutral culture. Make sure …. etc etc”. Fix this by clicking on the resx file in solution explorer and changing its Build Action from Content to Embedded Resource.. (thanks to http://www.devproconnections.com/article/aspnet22/ASP-NET-MVC-Localization for this point).

LocalizedDisplayNameAttribute class for Asp.Net MVC2

For whatever reason, in MVC2 the DisplayName attribute isn’t localizable in the way that ValidationAttributes are – it doesn’t have a constructor that looks up resources.

So here’s how to localise DisplayName:

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(Type resourceType, string resourceKey) : base(LookupResource(resourceType, resourceKey)) {  }
    public LocalizedDisplayNameAttribute(Type resourceType) : base(LookupResource(resourceType, DisplayNameAttribute.Default.DisplayName) ) { }

    internal static string LookupResource(Type resourceType, string resourceKey)
    {
        return new ResourceManager(resourceType).GetString(resourceKey) ?? resourceKey;
    }
}

Note! This will all fail dismally unless … your resource file is marked as public, rather than internal. Because Views are not compiled as part of the web project assembly, rather they are compiled at runtime by the asp.net compiler into a different assembly

A no-code routing service using .Net 4 on IIS

A year ago I was working with Waheed Hussain who pointed out that the .Net framework includes a Routing namespace which allows you to implement a service router with zero code. Literally. It’s an IIS application with no code, just a web.config. Here’s an example:

< ?xml version="1.0"?>
<configuration>
	<system.serviceModel>
		<routing>
			<filters>
				<filter name="MatchAll" filterType="MatchAll" />
			</filters>
			<filtertables>
				<filtertable name="MyFilterTable">
					<add filterName="MatchAllFilter" endpointName="MyDestinationEndpoint" priority="0"/>
				</filtertable>
			</filtertables>
		</routing>

		<services>
			<service behaviorConfiguration="routingConfiguration" name="System.ServiceModel.Routing.RoutingService">
				<endpoint address="destinationUrl/" binding="basicHttpBinding" name="routerEndpoint1" contract="System.ServiceModel.Routing.IRequestReplyRouter" />
			</service>
		</services>

		<client>
			<endpoint name="MyDestinationEndpoint"
					  address="https://Destination.1.Host.com/destinationUrl/"
					  binding="basicHttpBinding"
					  bindingConfiguration="basicHttpBindingWithClientCertificate"
					  behaviorConfiguration="clientEndpointCredential"
					  contract="*" />
		</client>

		<behaviors>
			<servicebehaviors>
				<behavior name="routingConfiguration">
					<routing routeOnHeadersOnly="true" filterTableName="MyFilterTable" />
					<servicedebug includeExceptionDetailInFaults="true" />
					<servicemetadata httpGetEnabled="true"/>
				</behavior>
			</servicebehaviors>
			<endpointbehaviors>
				<behavior name="clientEndpointCredential">
					<clientcredentials>
						<clientcertificate storeName="My" 
                               storeLocation="LocalMachine" 
                               x509FindType="FindBySubjectName" 
                               findValue="MyClientCertificate" />
					</clientcredentials>
				</behavior>
			</endpointbehaviors>
		</behaviors>

		<bindings>
			<basichttpbinding>
				<binding name="basicHttpBindingWithClientCertificate">
					<security mode="Transport">
						<transport clientCredentialType="Certificate"/>
					</security>
				</binding>
			</basichttpbinding>
		</bindings>

		<!-- The clever bit - activate with no code needed -->
		<servicehostingenvironment>
			<serviceactivations>
				<add relativeAddress="Destination.1.Host.com.svc" 
             service="System.ServiceModel.Routing.RoutingService, System.ServiceModel.Routing, version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			</serviceactivations>
		</servicehostingenvironment>
	</system.serviceModel>

	<system.web>
		<compilation debug="true" targetFramework="4.0" />
	</system.web>

	<system.net>
		<settings>
			<servicepointmanager expect100Continue="false" />
		</settings>
	</system.net>
	
</configuration>