如何在不使用Visual Studio的情况下实现一个ASP.NET MVC网站?

23

我看到了ASP.NET MVC Without Visual Studio,该问题问道:“是否可以在不使用Visual Studio的情况下基于ASP.NET MVC创建网站?”

被接受的答案是肯定的。

好的,下一个问题是:怎么做?


这里有一个比喻。如果我想创建一个ASP.NET Webforms页面,我会打开我最喜欢的文本编辑器,创建一个名为Something.aspx的文件,然后在文件中插入一些样板代码:

<%@ 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>

我也创建了Sourcefile.cs文件:

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

这是一个在文本编辑器中创建的可用的ASP.NET页面。将其放入IIS虚拟目录中,它就能工作。

如果我想要在文本编辑器中创建一个基本的"Hello, World" ASP.NET MVC应用程序(不使用Visual Studio),我需要做什么?

假设我想要一个带有控制器、一个视图和一个简单模型的基本MVC应用程序。我需要创建哪些文件,并将什么内容放入这些文件中?


1
我很欣赏这个问题的智力挑战,但我必须问一下,你为什么不想使用VS? - HitLikeAHammer
4
我一直用文本编辑器写很多应用程序,我喜欢了解创建的文件是什么以及为什么要创建。我并不反对使用VS,但我想知道,具体而言,我需要用文本编辑器做哪些事情。 - Cheeso
你是不是想不用编译器?因为这会对你设置文件夹和项目的方式产生很大的影响。 - Min
1
不,虽然那是一个优点。主要目标是不使用Visual Studio。如果需要的话,我并不介意为部署运行编译步骤。但在开发阶段自动编译也很好。 - Cheeso
4个回答

19

好的,我查看了Walther的教程并成功运行了一个基本的MVC网站。

所需文件包括:

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

就是这样。

在 Global.asax 中,我提供了以下样板:

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

那个MvcApplication类是在一个名为Global.asax.cs的模块中定义的,必须放置在App_Code目录中。内容如下:

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

Controller.cs提供处理各种请求的逻辑。在这个简单的示例中,控制器类如下:

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

控制器类的名称必须命名为XxxxxController,其中Xxxxx部分定义了URL路径中的段。对于名为HelloWorldController的控制器,URL路径段是HelloWorld。在控制器类中的每个公共方法都是一个动作,当该方法名包含在URL路径的另一个段中时,就会调用该方法。因此,对于上述控制器,这些URL将导致调用各种方法:

  • http:/ /server/root/HelloWorld(默认“动作”)
  • http:/ /server/root/HelloWorld/Index(与上面相同)
  • http:/ /server/root/HelloWorld/English
  • http:/ /server/root/HelloWorld/Italiano
  • http:/ /server/root/HelloWorld/Sample(视图,实现为Sample.aspx)

每个方法都返回一个操作结果,可以是以下之一:View(aspx页面)、Redirect、Empty、File(各种选项)、Json、Content(任意文本)和Javascript。

视图页面,例如此示例中的Sample.aspx,必须派生自System.Web.Mvc.ViewPage

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

就这样!将上述内容放入IIS vdir中,即可获得一个工作的ASPNET MVC网站。

(好吧,我还需要web.config文件,其中有8k的配置。 所有这些源代码和配置都可以浏览或下载。

然后我就可以添加其他静态内容:js、css、图像和任何我喜欢的东西。


web.config 的链接有问题。是否有其他地方可以查看这样一个简单的 MVC 应用程序的 web.config - Richard JP Le Guen
没错 - 那个链接是坏的。现在已修复。 - Cheeso
这是一个很棒的教程。谢谢Cheeso! - Animesh
你的 Global.asax 引用了 MvcApplication1.MvcApplication,但是在 Global.asax.cs 中你创建了一个全局命名空间下的 MvcApplication。你需要将其中一个改为与另一个匹配。 - Idan Arye

3
你需要做的就是像上面那样操作,因为在一个简单的hello world应用程序中,你不需要使用模型或控制器。
Visual Studio只是提供文件创建向导,理论上来说,你只需要创建正确的文件即可。如果你想要详细的MVC项目结构规范,祝你好运,大多数文档都是基于你使用Visual Studio的假设条件编写的,但你可以尝试一步一步地通过教程理解它。
你最好的选择是找到可下载的演示项目,使用Visual Studio反向工程该项目结构,或尝试使用开源的.NET IDE之一。

Mike,你的回答无效了问题。让我重新表述一下。假设我想要一个基本的MVC应用程序,其中包括一个控制器,一个视图和一个简单的模型。我需要创建哪些文件,以及它们需要包含什么内容? - Cheeso
@mikerobi:Visual Studio不仅提供文件创建向导(例如内置Web服务器),还有更多功能。当然,你可以不用Visual Studio创建ASP.NET MVC应用程序,但我不会低估VS的价值。 - reustmd
@Cheeso 我只是指在构建一个可工作的项目所需的上下文中。就个人而言,如果没有 Visual Studio 的调试器,我永远不会做任何 ASP.net 项目。 - mikerobi
实际上,Mike,我采纳了你的建议,从Walther下载了一个样例,并检查了源文件。现在我有了自己的ASPNET MVC模板项目。感谢你的建议,点个赞! - Cheeso

2

这是MVC 1.x应用程序默认的VS骨架样式:

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

我不确定这是否符合您的要求……关键在于web.config文件。


1
我认为你只需要复制这个结构。值得一提的是,还应该查看Global.asax文件的内容,因为这里设置了非常重要的路由。http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-05-cs.aspx有一个例子。 - Damovisa
这是我需要的一部分。当然,我还需要每种文件类型的代码框架信息。此外 - 这些是项目中的源文件。源文件结构如何与部署站点结构相关?ASPNET MVC项目是否只是将所有C#代码编译成DLL并将其放入站点的bin目录中? - Cheeso

1
注意:如果您添加了命名空间,必须拥有一个程序集。
在Mono项目下的opensuse linux上进行Cheeso示例项目的web.config示例。
<?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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接