Koen about .Net

July 25, 2011

/temp/global.asax(1,0): error ASPPARSE: Could not load type

Filed under: Deployment — Tags: , , — koenwillemse @ 10:00

Since it’s my vacation I have some more time to work on my home project. I’m creating a web shop in ASP.NET MVC 3 and I’ve got a test TFS server running. I use this project to get some hands on experience in ASP.NET MVC 3 since the project I’m working on is still Webforms but I see more future in MVC. I’ve implemented a basic site where you get a view a list of products and some more details about them.

So my next step is that I want a CI build on the TFS server. I configured it and it seems pretty easy, but than I got the following error in my CI build (but I did not get it when building locally): “/temp/global.asax(1,0): error ASPPARSE: Could not load type …..”.

Eventually I found the problem. In the log file I saw the call to aspnet_compile, which is triggered because I set the property to build the views (MvcBuildViews). The problem is with the path which is supplied to the aspnet_compile command. I eventually fixed it by adding a condition to the AspNetCompiler build task in my web application project file. It now looks like this:

 <Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
     <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)" Condition="'$(IsDesktopBuild)' == 'true'" />
     <AspNetCompiler VirtualPath="temp" PhysicalPath="$(OutDir)\_PublishedWebsites\$(ProjectName)" Condition="'$(IsDesktopBuild)' == ''" />
 </Target>

To get this working I defined a build property IsDektopBuild which I set to true in the Debug build configuration, which is the configuration I’ll build locally. I also defined a Debug CI configuration which does not contain this build property. Now I’ve got my CI build up and running. Next step is to add some specflow acceptance tests to see if they help me with controlling the quality of the code.

Advertisements

January 22, 2011

Culture specific website in ASP.NET MVC

Filed under: Development — Tags: , — koenwillemse @ 00:36

In my personal time I’m working on a web shop application. It’s an application which is really going to be used, so not like the usual throw-away home projects. I could off course just grab an already existing application, but I’m too geeky for that Winking smile. I also wanted to use it to learn a lot about working with ASP.NET MVC and jQuery.

One of the requirements is that it have to support multiple languages. I wanted to do this the way you see it at for instance http://msdn.microsoft.com/nl-nl/ with the culture code in the url. So far so good. I started out by using a feature in ASP.NET MVC which I found, and that was by using a FilterAttribute in combination with an ActionFilter which I found here: http://helios.ca/2009/05/27/aspnet-mvc-and-localization/. Nice solution I thought, so I took that code, modified it a bit to match my scenario and it worked.

But then when I added more views to the application, I started to notice that it was not a good solution at all, because of the following reasons:

  1. I had to change my routes so they would work correctly with the culture code in the url (urls will look like this: http://www.pastechi.nl/nl-NL/Products/Index etc). Maybe this wasn’t necessary and it is because I’m not completely familiar with the routing in ASP.NET, but still, it was not what i wanted.
  2. Every action on my controller now had a parameter cultureCode, which was not used in that method, because the culture code was set in the FilterAttribute code.
  3. It just felt wrong Winking smile

So I started thinking about it and I came to the conclusion that I just brought a knife to a gun fight. The solution works, but is not very usefull. So what would be better? Well, the new solution I’ve got implemented uses url rewriting. The url rewriting is done using the Application_BeginRequest event and takes place before the routing engine of ASP.NET MVC is doing it’s magic. So I created a simple HttpModule which can be used to rewrite the url, by removing the culture code from the url and placing the culture code in a suitable location.

This is the code I wrote (keep in my that this is the POC code, so it should be rewritten a bit to be unit testable etc).

using System;
using System.Text.RegularExpressions;
using System.Web;

namespace HttpModules
{
    public class CulturePathRewriteModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += OnBeginRequest;
        }

        static void OnBeginRequest(object sender, EventArgs e)
        {
            var request = HttpContext.Current.Request;
            var cultureRegEx = new Regex(@"(^\/[A-z]{2}\-[A-z]{2})|(^\/[A-z]{2}\-[A-z]{2}.*)");

            if (cultureRegEx.IsMatch(request.Url.AbsolutePath))
            {
                string cultureCode = request.Url.AbsolutePath.Remove(0, 1).Substring(0, 5);
                string newAbsolutePath = request.Url.AbsolutePath.Remove(0, 6);
                string newUrl = "~" + newAbsolutePath;

                HttpContext.Current.Items["currentCultureCode"] = cultureCode.ToLower();
                HttpContext.Current.RewritePath(newUrl);
            }
        }

        public void Dispose()
        {
        }
     }
}

You probably noticed that I didn’t set the CurrentCulture / CurrentUICulture on the current thread. It’s not that I don’t know that it exists, but I can’t use it. One of the languages that I want to support doesn’t have an official CultureCode. Off course, you can create this if you want, but because the site will be running on a shared hosting partner, that is not an option. The eventual code won’t place it in the context using a hardcoded key, but it will be done using a custom wrapper on the context which I can use typed, but you’ve got the idea.

July 25, 2010

First steps with ASP.NET MVC and ASP.NET routes

Filed under: Development — Tags: — koenwillemse @ 00:31

I’ve started working on an ASP.NET MVC 2 project recently and that means that there is a lot of new stuff to learn ;-).

Previous ASP.NET projects I’ve worked on were all based on Webforms and almost all ‘AJAX’ stuff which we used were UpdatePanels and the AJAX Control toolkit. I must say that I start to like ASP.NET MVC better than Webforms. It’s a different way of working, but when you get used to it, it works great. I also started to work with jQuery and it’s actually very easy. It’s a very interesting last few days, that’s for sure.

But the main reason I write this blog post is the routing stuff. I created some routes for my project and it was not working the way I wanted. Strange stuff was happening. Thankfully I remembered the talk of Scott Hanselmann on the DevDays 2010 about ASP.NET MVC. He mentioned the routing debugger written by Phil Haack. I download the dll, added it as a reference, fired up my application and within several minutes I found my problem and fixed it. Very usefull tool, that’s for sure!

Blog at WordPress.com.

%d bloggers like this: