Wednesday, June 3, 2009

Why does ASP.NET MVC have to be a 'web application'? REDUX

Recently I wrote a post asking the question ‘Why does ASP.NET MVC have to be a web application?’ so here’s

Step 1: Using Visual Studio 2008, Select File->New->Project

step1

Step 2: Select the ‘ASP.NET MVC Web Application’ project template

step2

Step 3: Select ‘No, do not create a unit test project’ when prompted to create a unit test project

step3

Step 4: Close the newly created ASP.NET MVC Web Application' by selecting File->Close Solution

step4

Step 5: Select File->New->Project and under ‘Other Project Types\Visual Studio Solutions’, select Blank Solution and create an empty solution

step5

Step 6: Right-click on the newly created solution and select ‘Add->Existing Web Site’

step6

Step 7: Use the File System option to navigate to the location of the previously created ASP.NET MVC Web Application

step7

Step 8: Delete the *.csproj* files and the ‘bin’ and ‘obj’ directories.

step8

Step 9: Right-click on the web site and select ‘Add ASP.NET Folder->App_Code’, then drag-and-drop the Controllers folder into the App_Code directory

step9 step10

Step 10: Double-click on the Default.aspx file and change the CodeBehind attribute to CodeFile

step11a

step11b

Step 11: Open the Global.asax.cs file and copy the RegiterRoutes method.

Step 12: Delete the Global.asax and Global.asax.cs file. Create a new ‘Global Application Class'.’ Paste the RegisterRoutes method into the Global.asax file and add the call to the register routes method to the Application_Start method.

<%@ Application Language="C#">
<script runat="server">
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}

void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>


At this point you should be ready to go. Remember, that the reason Microsoft decided to make ASP.NET MVC projects web applications by default is to allow for unit testing more easily. Good Luck!

0 comments:

Post a Comment