Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a site with ASP.NET MVC without using Visual Studio?

Tags:

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?

like image 995
Cheeso Avatar asked Mar 15 '10 23:03

Cheeso


People also ask

How do I open an ASP.NET project without Visual Studio?

To run the application without opening Visual Studio, You need to install IIS (as rtpHarry has suggested). Then You need to have your application in a virtual directory. To create virtual directory perform following steps: Type inetmgr at command prompt -> Click OK.

Can we run .NET application without Visual Studio?

All replies. You can host your application on iis and run without visual studio. Create a windows batch file to start the ASP.NET Web Development Server and point it to your application's root directory. To open aspx page you need a ASP.NET server.

Can we create MVC application without model?

It can be used in the view to populate the data, as well as sending data to the controller. If you have simply data to present, you can create view without model. Here is the introduction of ASP.NET MVC Pattern and the overview of ASP.NET Core Model-View-Controller(MVC).


2 Answers

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

The files required were:

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

That's it.

Inside the Global.asax, I provide this boilerplate:

<%@ Application Inherits="MvcApplication1.MvcApplication" Language="C#" %> 

And that MvcApplication class is defined in a module called Global.asax.cs which must be placed into the App_Code directory. The contents are like this:

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);     } } 

The Controller.cs provides the logic to handle the various requests. In this simple example, the controller class is like this:

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 be named XxxxxController, where the Xxxxx portion defines the segment in the URL path. For a controller called HelloWorldController, the URL path segment is HelloWorld. Each public method in the Controller class is an action; the method is called when that method name is included in another segment in the url path . So for the above controller, these URLs would result in invoking the various methods:

  • 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, such as Sample.aspx in this case, 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. All this source code and configuration is available to browse or download.)

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

like image 139
Cheeso Avatar answered Sep 29 '22 12:09

Cheeso


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.

like image 42
mikerobi Avatar answered Sep 29 '22 10:09

mikerobi