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

Posted

技术标签:

【中文标题】如何在不使用 Visual Studio 的情况下使用 ASP.NET MVC 实现站点?【英文标题】:How can I implement a site with ASP.NET MVC without using Visual Studio? 【发布时间】:2011-01-27 21:53:32 【问题描述】:

我见过ASP.NET MVC Without Visual Studio,它问, 是否可以不使用 Visual Studio 来制作基于 ASP.NET MVC 的网站?

接受的答案是,是的

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


这是一个类比。如果我想创建一个 ASP.NET Webforms 页面,我加载 my favorite text editor,创建一个名为 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
        
    

是一个工作的 ASPNET 页面,在文本编辑器中创建。将它放到一个 IIS 虚拟目录中,它就可以工作了。

我需要做什么,才能在文本编辑器中制作一个基本的 hello, World ASPNET MVC 应用程序? (没有 Visual Studio

假设我想要一个带有控制器、一个视图和一个简单模型的基本 MVC 应用程序。我需要创建哪些文件,它们会包含什么?

【问题讨论】:

我很欣赏这个问题的智力挑战,但我必须问,你为什么不想使用 VS? 我在文本编辑器中编写了很多应用程序,一直都有。我想了解正在创建哪些文件以及创建原因。我对VS没有任何反对意见。但我想知道需要什么,特别是我用文本编辑器来做。 您是否尝试不使用编译器?因为这将对您设置文件夹和项目的方式产生巨大影响。 不,虽然 加分项。主要目标是不使用 Visual Studio。如果有必要,我不介意运行编译步骤进行部署。但是自动编译也很好,尤其是在开发阶段。 【参考方案1】:

好的,我检查了Walther's tutorial 并运行了一个基本的 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
        
    

Controller 类必须命名为XxxxxController,其中 Xxxxx 部分定义 URL 路径中的段。对于名为HelloWorldController 的控制器,URL 路径段是HelloWorld。 Controller 类中的每个公共方法都是一个action;当该方法名称包含在 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)

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

View 页面(例如本例中的 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 的配置。All this source code and configuration is available to browse or download.)

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

【讨论】:

web.config 的链接有问题。还有其他地方我可以查看web.config 以获得如此简单的 MVC 应用程序吗? 是的 - 这是一个错误的链接。现在已修复。 这是一个很棒的教程。谢谢芝士! 您的Global.asax 指的是MvcApplication1.MvcApplication,但在Global.asax.cs 中,您在全局命名空间中创建MvcApplication。你需要改变一个以适应另一个。【参考方案2】:

您将完全按照上面所做的那样做,因为您不会在 hello world 应用程序中使用模型或控制器。

Visual Studio 所做的只是为您提供文件创建向导,因此理论上,您需要做的就是创建正确的文件。如果您想了解 MVC 项目结构的详细规范,祝您好运,大多数文档都是在您使用 Visual Studio 的前提下编写的,但您可能可以逐步完成教程,然后解开谜题。

最好的办法是找到一个可下载的演示项目,使用 Visual Studio 对项目结构进行逆向工程,或者尝试其中一种开源 .net IDE。

【讨论】:

迈克,你的回答使问题无效。让我重新定义它。假设我想要一个带有控制器、一个视图和一个简单模型的基本 MVC 应用程序。我需要创建哪些文件,其中包含哪些内容? @mikerobi:Visual Studio 不仅仅是为您提供文件创建向导(例如,内置 Web 服务器)。当然不用visual studio也可以创建asp.net mvc app,但是VS我不会卖空的。 @Cheeso 我的意思只是在构建一个工作项目所必需的上下文中。就个人而言,如果没有豪华的 Visual Studio 调试器,我永远不会做任何 ASP.net 项目。 实际上,Mike,我接受了您的建议,并从 Walther 下载了一个示例,并检查了源文件。现在我有了自己的 ASPNET MVC 模板项目。感谢您的建议,并投赞成票!【参考方案3】:

嗯,这就是 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 文件。

【讨论】:

我想说你真的只需要复制这个结构。值得查看 Global.asax 文件的内容以及设置 非常重要 路由的位置。 asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/… 有一个例子。 这是我需要的一部分。然后,当然我还需要有关每种文件类型的代码骨架的信息。另外 - 这些是项目中的源文件。该源文件结构与部署站点结构有何关系? ASPNET MVC 项目是否只是将所有 C# 代码编译成 DLL 并将其放入站点的 bin 目录中?【参考方案4】:

注意:如果你添加了命名空间,你必须有一个程序集。

web.config 示例,用于在 mono 项目下的 opensuse linux 上 Cheeso 示例项目。

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

【讨论】:

以上是关于如何在不使用 Visual Studio 的情况下使用 ASP.NET MVC 实现站点?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不删除目标文件夹内容的情况下使用 Visual Studio 2008 中的“发布”功能?

如何在不使用 Visual Studio 的情况下直接在 Docker 容器上运行 ASP.NET Core Web 应用程序

如何在不安装 Visual Studio C++ 的情况下安装人脸识别

如何在不显式编译的情况下让 Visual Studio 错误检查我的代码(显示曲线)?

如何在不使用 Visual Studio 作为先决条件的情况下使用 node js javascript 连接到 oracle 数据库

是否可以在不购买 Visual Studio 许可证的情况下使用团队资源管理器进行版本控制?