Amptools.Net

simplify your life.

Archive for October, 2009

Asp.Net Mvc 2 Areas Tip: Drop in a web.config

No Gravatar

I ran into an exception “Could not load type ‘System.Web.Mvc.ViewPage” or something along those lines while trying to implement “Areas” for Asp.Net MVC 2.

So if you are coming up against this issue, you might be having that same issue.

The solution, drop in a copy of the web.config from the views folder into the folder for your areas folder (i.e. Areas/AreaName or Areas/AreaName/Views). Refresh. That should solve that exception. Hopefully.

Tags: , , ,

Switching between Ioc libraries in Net.

No Gravatar

Lets face it. Some developers are religiously attached to their beloved languages, frameworks, and libraries.

This becomes a problem when building software with hopes of gaining a developer base. If it is not built on Unity, Castle Windsor, Structure Map or [insert beloved Ioc library here], they refuse to work/use the software.

Evidently others have had this issue and have created a solution, the common service locator.  I ran across while searching for asp.net  mvc and DI (dependency injection) for models. More on DI with models inside controllers in a future post.

How to use

Download the library from codeplex.com/CommonServiceLocator

Its pretty straight forward.  Download the service locator adapter for the Ioc library of your code on the codeplex.com/CommonServiceLocator

choose the adapter for your Ioc library.

choose the adapter for your Ioc library.

Configure your Ioc library in the usual manner. Then pass the container into the adapter.

        private IServiceLocator CreateLocator()
        {
            var container = new UnityContainer();
            container.RegisterType<IUser, User>();
            container.RegisterType<IMembership, Membership>();
            container.RegisterType<IUser, Loser>("loser");

            return new UnityServiceLocatorAdapter(container);
        }

Then tell the service locator which adapter you are using.

        private void SetupService()
        {
            // CreateLocator returns IServiceLocator in the above method.
            ServiceLocator.SetLocatorProvider(() => CreateLocator());
        }

Then use the locator to return the adapter, which will allow you to use a common interface to resolve your dependencies.

        [It, Should("resolve the type to the default")]
        public void ResolveType()
        {
            this.SetupService();
                         // gets the current adapter for the Ioc Library.
            var resolver = ServiceLocator.Current;             

            // resolves the type.
            var result = resolver.GetInstance(typeof(IUser));

            // verify the result is the right type for the contract.
            result.IsInstanceOf(typeof(User));

            // this user should not have a membership.
            var user = (User)result;
            user.Membership.ShouldBeNull();
        }

Some Caveats.

Someone named the base service locator class in a java like manner using Impl.

The assemblies for the Unity Ioc are not strong named assemblies. I had to create a key, sign the assign the assemblies for Unity. You can download the midori-project which has the signed assemblies for Unity under the vendor folder.

Further Usage and Reading

If you would like to check out the spec and see how it might be used, check out the spec in the midori core. This will give you some context.

You can also read more about it on codebetter.com as well as the initial post from Jeremy Miller that spawned someone to write this.

Tags: , , , , , , ,

Asp.Net Mvc Add Css Classes To The Body Element

No Gravatar

Using the css classes on the body tag/element, you can open a door to having finer control over the layout of your site. Its one of the few decent tricks that bigger Content/Course Management Software like drupal or moodle provides.

In Asp.Net Mvc this can be done in the master page, outside of the head tags. What makes this nice is using the controller and action name to isolate the page and section of the site you are int.

</head>
<%
        var values = Html.ViewContext.RouteData.Values;
        var bodyClasses = values["controller"] + " " +
                        values["action"];
%>
// in the master page
<body class="<%= bodyClasses %>"> 

// what should be rendered for the url: /blog/edit/5
<body class="blog edit">

As you can see above, it would pay off to stick to using the controller and action keywords in your routing. You could adjust the above for adding the modules/areas name.

.blog aside.column {
        display: none;
}

.blog.edit  footer {
        margin-top: 20px;
        background: transparent(images/egg.png) no-repeat;
}

You can now target certain aspects of the layout and change it depending what section of the site you are in.

Tags: , , , , , , , ,

Missing ValidationMessageFor ?

No Gravatar

Looking for the method ValidationMessageFor() ? Or at least wondering what happened to it? Same here.

I see the example of it’s use on Scott Gu’s Blog for Asp.Net Mvc 2.0 preview 1. Yet I have not been able to google/bing a blog, tweet, or official mention of why this was dropped.

I checked the bits using , source code, and release notes for . The bits and source code do confirm one thing. The extension methods for ValidationMessageFor() are not available in Preview 2.

Unfortunately I do not have a bat phone (or a direct message on twitter) to anyone that works on Asp.Net Mvc. But I’m sure someone will pick up on this.

Tags: , , , ,