Friday, April 17, 2009

DynamicData cleanup

Last time I've described a way of merging the DynamicData capability with an ASP.NET MVC application. This time I'll show you how to make the DynamicData part be stored in the right place.

First of all it's kind of awkward to have the Site.css, Site.master and the rest in the root folder. Let's face it: this sucks! It'd be nice if we could have those DynamicData-related stuff inside the DynamicData folder where they could live happily ever after.

It's not impossible but there are two small glitches that you need to be aware of. But first things first.

Step 1. Grab the Site.css, Site.master in solution explorer and drag them to the DynamicData folder.

Step 2. Open the DynamicData\Site.master file and change the

<img alt="Back to home page" runat="server" src="DynamicData/Content/Images/back.gif" />

with

<img alt="Back to home page" runat="server" src="~/DynamicData/Content/Images/back.gif" />

Node the Tilda addition in the image src attribute!

Step 3: Change the master page for pages. In the folder DynamicData\PageTemplates you'll find templates for all the generated pages. Open them one by one (there are 5 of them: Details.aspx, Edit.aspx, Insert.aspx, List.aspx and ListDetails.aspx) and change the MasterPageFile attribute from

MasterPageFile="~/Site.master"

to

MasterPageFile="~/DynamicData/Site.master"

That's it! There's nothing more to it. From now on your application structure is cleaner and there's noting polluting the clear ways of the ASP.NET MVC project!


Have fun!

Adding DynamicData to an ASP.NET MVC application

In my previous post I've pointed you to a place that contains a ready-to-use solution on how to use DynamicData with ASP.NET MVC. In this post I'll describe the manual way of doing that so that you can see that there's no mystery hiding behind the scenes.

I'm going to assume that you have an up-and-running project created using ASP.NET MVC Application template.

Step 1: Create a separate DynamicData project. Call it whatever you want - we're going to need a few files from that project.

Step 2: Copy the following files from the DynamicData project right into the MVC application and include them in the MVC project:

  • /Site.css

  • /Site.master

  • /Site.master.cs

  • /Site.master.designer.cs

  • /DynamicData (the whole folder!)


Step 3: We need to register the DynamicData engine in our application. To do that copy the following lines from the Web.config file in DynamicData project into the Web.config file in ASP.NET MVC project:

  • from \\configuration\system.web\compilation\assemblies
    <add assembly="System.Web.DynamicData, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  • from \\configuration\system.web\pages\controls
    <add tagPrefix="asp" namespace="System.Web.DynamicData" assembly="System.Web.DynamicData, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Step 4: Add a reference to System.Web.DynamicData to the MVC project.

Step 5: Register routes in Global.asax.cs. Depending on the context you'll have to adjust the context registration. Here I'm using the Northwind data context:

MetaModel model = new MetaModel();
model.RegisterContext(
typeof(MvcApplication1.Models.NorthwindDataContext),
new ContextConfiguration() {
ScaffoldAllTables = true
});
routes.Add(
new DynamicDataRoute("DD/{table}/{action}.aspx") {
Constraints = new RouteValueDictionary(
new { action = "List|Details|Edit|Insert" }),
Model = model
});

Make sure you add the proper using statement to System.Web.DynamicData and your models!

Step 6: Add a listing of all the tables to your Views/Home/Index.aspx:

<% foreach (MetaTable table in MetaModel.Default.Tables) { %>
<li>
<a href="<% =table.GetActionPath("List") %>">
<% =table.DisplayName %>
</a>
</li>
<% } %>


Done!

Run your application. On the initial page you should see a clickable list of all the tables from your database.

Enjoy!

DynamicData with ASP.NET MVC

Recently while browsing the web for interesting resources I've stumbled upon a blog entry talking about DynamicData. Since I've had no idea what that was I've dug a little bit into it and found out that it poses a fantastic opportunity to complement an otherwise complete site with a nice browser/editor for the whole database.

Since it was all nice and dandy I've dug a little bit more trying to integrate this technology with the ASP.NET MVC framework. And as it turns out there's a great example here that provides an end-to-end solution.

Since I've had some trouble running this example out-of-the-box I though I'd share what I've done to make it work:

1. Open the ~/Views/Shared/Site.Master
2. Add the following line right after the page declaration:

<%@ Import Namespace="System.Web.Mvc.Html" %>


That's it! Just hit F5 and observe the beauty of the DynamicData integrated with ASP.NET MVC :D

Have fun!

Linking resources in ASP.NET MVC

Working on web sites that have dynamic URLs (like the ones in ASP.NET MVC or JSF) poses one difficulty: you never know where to look for resources such as images or CSS files. This is due to the fact that the same view can have more than one address. Let me give you an example:

/Demo/ -> this should render the default controller (HomeController) with the default view (Index.aspx).
/Demo/Home -> this should render the specified controller (again HomeController) with the default view (Index.aspx)
/Demo/Home/Index -> this should render the specified controller (again HomeController) with the specified view (Index.aspx)

So how do you go about specifying an URL for resources in this case? Well there are different ways to do that.

1. You could create and use an Html helper method to do that.
2. You could hard-code the absolute paths (like /Demo/Content/Index.css for example)
3. You could make use of the runat attribute and use relative paths.

I personally dislike the first and second options. They tend to introduce mess to my otherwise clear code. The third option is in my opinion the best one.
Here's an example of how you'd create a link to a CSS:


<link type="text/css" href="~/Content/Site.css" runat="server" />


Happy coding!

Wednesday, April 15, 2009

NUnit template for ASP.NET MVC

Hi there,

I've been struggling a bit with the automatic addition of NUnit unit tests to ASP.NET MVC project in Visual Web Developer. I wanted to have it the same way it works with the VisualStudio unit test framework that is being shown over and over again on the net.

So here's the complete solution: just unzip it, run install.bat and enjoy!

ASP.NET-MVC-NUnitTemplate.zip

Additionally to the standard files you'd expect to be in place there's a NUnit project file called UnitTests.nunit. It's already configured and ready to go. To open it with the NUnit test runner do a right click on it, select "Open with...", add a new program by using the "Add" button, select the nunit.exe file, click ok, highlight NUnit in the list, click "Set as Default" and click OK. From now on everytime you double-click on it you'll run the NUnit GUI test runner which is pretty much what you'd expect.

Padcom.

Sunday, March 8, 2009

Running JSP/Servlets under .NET in C#

Hi there,

Recently I've been amazed by the things you can accomplish by using the IKVM library. I took me quite a while to realize the actual potential that hides behind this kind of integration that IKVM provides (which is kind of stupid as I'm working in both technologies, just not at the same time).

Let's get to the point!

Here's a line that will compile jetty (tried with version 6.1.11) into a .NET assembly with everything required to start writing and hosting JSP and servlets:

ikvmc -target:library jetty-6.1.11.jar jetty-util-6.1.11.jar servlet-api-2.5-6.1.11.jar jsp-2.1\core-3.1.1.jar jsp-2.1\ant-1.6.5.jar jsp-2.1\jsp-2.1.jar jsp-2.1\jsp-api-2.1.jar

That produces a .NET assembly called jetty-6.1.11.dll which you should include along with IKVM.OpenJDK.ClassLibrary.dll and IKVM.Runtime.dll to make it work.

And now for the fun stuff! Here's a code snippet (actually it's a complete application) that starts up jetty, creates 2 contexts (one for the servlet under /servlet and one for the example web application in JSP under /jsp) and starts the server:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using java.io;
using java.lang;
using javax.servlet.http;
using org.mortbay.jetty;
using org.mortbay.jetty.servlet;
using org.mortbay.jetty.webapp;

namespace JettyServletAndJSPExample {
class Program {
static void Main(string[] args) {
Server server = new Server(8080);

// see: http://localhost:8080/servlet/whatever
Context servletContext = new Context(server, "/servlet", Context.SESSIONS);
servletContext.addServlet(new ServletHolder(new TestServlet()), "/*");

// see: http://localhost:8080/jsp/hello.jsp
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/jsp");
webapp.setWar("../../webapps/test");
webapp.setDefaultsDescriptor("../../etc/webdefault.xml");
server.addHandler(webapp);

server.start();
}
}

class TestServlet : HttpServlet {
protected override void doGet(HttpServletRequest req, HttpServletResponse resp) {
resp.setContentType("text/html");
PrintWriter o = resp.getWriter();
o.println("<h1>hello, world!</h1>");
o.close();
}
}
}


After that you have a full fledged, java-like (very much alike) server that can hosts servlets written in C# for .NET platform and at the same time to host applications written in pure JSP.

The obvious next step will be to extract the rendering engine alone to be used with ASP.NET MVC framework thus enriching the seamless integration between Java and the .NET world.

If you're lazy enough you can download the whole package here and start exploring it immediately.

Have fun!

Tuesday, January 13, 2009

Spring - a "brief" introduction

Hi there,

I was looking for a "getting started", "hello-world" or something of that sort for Spring. Imagine my surprise when I found this:

SPR-101: Getting Started With Spring Training
Submitted by class editor on Mon, 12/22/2008 - 16:16.

Getting Started With Spring is a two-day training course focused toward experienced Java and JEE developers who need a brief, yet comprehensive, course to get them “up and running “ and writing Spring-powered applications in as little time as possible.


OMG! 2 days of study needed just to get me started on one library?! WTF!!!