views:

378

answers:

3

I have seen ASP.NET MVC Without Visual Studio, which asks, Is it possible to produce a website based on ASP.NET MVC, without using Visual Studio?

And the accepted answer is, yes.

Ok, next question: how?


Here's an analogy. If I want to create an ASP.NET Webforms page, I load up my favorite text editor, create a file named Something.aspx. Then I insert into that file, some boilerplate:

<%@ Page Language="C#"
  Debug="true"
  Trace="false"
  Src="Sourcefile.cs"
  Inherits="My.Namespace.ContentsPage"
%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Title goes here </title>
    <link rel="stylesheet" type="text/css" href="css/style.css"></link>

    <style type="text/css">
      #elementid {
          font-size: 9pt;
          color: Navy;
         ... more css ...
      }
    </style>

    <script type="text/javascript" language='javascript'>

      // insert javascript here.

    </script>

  </head>

  <body>
      <asp:Literal Id='Holder' runat='server'/>
      <br/>
      <div id='msgs'></div>
  </body>

</html>

Then I also create the Sourcefile.cs file:

namespace My.Namespace
{
    using System;
    using System.Web;
    using System.Xml;
    // etc... 

    public class ContentsPage : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Literal Holder;

        void Page_Load(Object sender, EventArgs e)
        {
            // page load logic here
        }
    }
}

And that is a working ASPNET page, created in a text editor. Drop it into an IIS virtual directory, and it's working.

What do I have to do, to make a basic, hello, World ASPNET MVC app, in a text editor? (without Visual Studio)

Suppose I want a basic MVC app with a controller, one view, and a simple model. What files would I need to create, and what would go into them?

+2  A: 

You would do exactly what you did above, because you wouldn't use a model or controller in a hello world app.

All visual studio does is provide you with file creation wizards, so in theory, all you need to do is create the right files. If you want detailed specifications for the MVC project structure, good luck, most documentation is written on the assumption you are using visual studio, but you might be able to go through a tutorial step by step, and puzzle it out.

Your best bet is to find a downloadable demo project, use visual studio to reverse engineer the project structure, or try one of the open source .net IDE.

mikerobi
Mike, your answer nullifies the question. Let me reframe it. suppose I want a basic MVC app with a controller, one view, and a simple model. What files would I need to create, and what would go into them?
Cheeso
@mikerobi: visual studio does much much more than provide you with file creation wizards (e.g., built-in web server). Of course you can create an asp.net mvc app without visual studio, but I wouldn't sell VS short.
manu08
@Cheeso I only meant in the context of what is necessary to build a working project. Personally, I would never do any ASP.net projects without the luxury Visual Studio's debugger.
mikerobi
Actually Mike, I took your suggestion and downloaded a sample from Walther, and examined the source files. Now I have my own template project for ASPNET MVC. Thanks for the suggestion, and have an upvote!
Cheeso
+1  A: 

Well, this is how the default VS skeleton for an MVC 1.x app looks like:

Content
 Site.css
Controllers
 AccountController.cs
 HomeController.cs
Models
Scripts
 (all the jquery scripts)
 MicrosoftAjax.js
 MicrosoftMvcAjax.js
Views
 web.config
 Account
  ChangePassword.aspx
  ChangePasswordSuccess.aspx
  LogOn.aspx
  Register.aspx
 Home
  About.aspx
  Index.aspx
Shared
 Error.aspx
 LogOnUserControl.ascx
 Site.master
Default.aspx
Global.asax
web.config

Dunno if that's what you're looking for... the key here is obviously the web.config file.

kprobst
I'd say you really just need to copy this structure. It's worth looking at the contents of the Global.asax file as well as this is where the *very important* routes are set. http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-05-cs.aspx has an example.
Damovisa
That's part of what I need. Then of course I also need the information about the code skeleton for each type of file. Also - those are the source files in the project. How does that source file structure relate to the deployed-site structure? Does an ASPNET MVC project just compile all the C# code into a DLL and drop it into the site's bin directory?
Cheeso
+6  A: 

ok, I examined Walther's tutorial and got a basic MVC site running.

The files required were:

Global.asax
web.config
Views\HelloWorld\Sample.aspx
App_Code\Controller.cs

That's it.

Inside the Global.asax, I need to provide

using System.Web.Mvc;
using System.Web.Routing;

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                      // Route name
            "{controller}/{action}/{arg}",  // URL with parameters
            new {                           // Parameter defaults
              controller = "HelloWorld",
              action = "Index", 
              arg = "" }                 );
    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

Inside the Controller.cs, I provide:

using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
    public class HelloWorldController : Controller
    {
        public string Index()
        {
            return "Hmmmmm...."; // coerced to ActionResult
        }

        public ActionResult English()
        {
            return Content("<h2>Hi!</h2>");
        }

        public ActionResult Italiano()
        {
            return Content("<h2>Ciao!</h2>");
        }

        public ViewResult Sample()
        {
            return View();  // requires \Views\HelloWorld\Sample.aspx
        }
    }
}

The Controller class must eb named XxxxxController, and the Xxxxx portion defines a segment in the URL path. Each public method in the Controller is an action, which is accessible via another url path segment. So for the above controller, I would get these URLs:

  • http:/ /server/root/HelloWorld (the default "action")
  • http:/ /server/root/HelloWorld/Index (same as above)
  • http:/ /server/root/HelloWorld/English
  • http:/ /server/root/HelloWorld/Italiano
  • http:/ /server/root/HelloWorld/Sample (a view, implemented as Sample.aspx)

Each method returns an Action result, one of the following: View (aspx page), Redirect, Empty, File (various options), Json, Content (arbitrary text), and Javascript.

The View pages, like Sample.aspx, must derive from System.Web.Mvc.ViewPage.

<%@ Page Language="C#"
  Debug="true"
  Trace="false"
  Inherits="System.Web.Mvc.ViewPage"
 %>

That's it! Dropping the above content into an IIS vdir gives me a working ASPNET MVC site.

(Well, I also need the web.config file, which has 8k of configuration in it.)

And then I can add other static content: js, css, images and whatever else I like.

Cheeso