Saturday, April 18, 2009

Visual Studio Web Developer Server and PHP

Today I've been looking for a solution that would allow me to serve PHP pages from my web application. Unfortunately it turns out there's no default handler for CGI applications. However Ziya Suzen has already created an IHttpHandler that does the job.

Remember: all the credits for this solution go to Ziya Suzen


PHP-Example.zip

Here you can find a complete solution that does just that: when executed from within Visual Web Developer it shows a list of files in a browser and when you select index.php you'll get a nice text "Hello, world!" that's being generated by PHP.

A few notes about the modifications I've made:

1. The php-cgi.exe executable has a fixed path build in. It's X:\programs\php\php-cgi.exe.
2. For some (yet) unknown reason the PHP script processor complains when the REQUEST_METHOD variable is set so I've commented it out.

Have fun!

PHP on MS platform???

Hi,

it looks like that MS is hitting at the PHP web page developers out there by providing a unified installer for PHP and PHP-based applications in their web-platform installer. Check it out here

Enjoy!

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.