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

前端 未结 4 2048
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 13:38

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 a

相关标签:
4条回答
  • 2020-12-04 14:00

    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.

    0 讨论(0)
  • 2020-12-04 14:19

    Note: if you added namespace you must have an assembly.

    web.config example for Cheeso example project on opensuse linux under mono project.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
      </configSections>
      <appSettings>
        <add key="webpages:Version" value="1.0.0.0" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
      <system.web>
        <customErrors mode="Off"/>
        <compilation debug="true" targetFramework="4.0">
          <assemblies>
            <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <!--        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> -->
          </assemblies>
        </compilation>
        <authentication mode="None"></authentication>
        <pages>
          <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
    <!--        <add namespace="System.Web.Helpers" />
            <add namespace="System.Web.WebPages" /> -->
          </namespaces>
        </pages>
        <httpHandlers>
          <add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" />
        </httpHandlers>
      </system.web>
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true" />
        <handlers>
          <add name="dotless" path="*.less" verb="*" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />
        </handlers>
      </system.webServer>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Abstractions" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <dotless minifyCss="false" cache="true" web="false" />
    </configuration>
    
    0 讨论(0)
  • 2020-12-04 14:24

    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.

    0 讨论(0)
  • 2020-12-04 14:25

    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.

    0 讨论(0)
提交回复
热议问题