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

Che*_*eso 23 asp.net asp.net-mvc

我见过没有Visual Studio的ASP.NET MVC,它问, 是否有可能在不使用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>
Run Code Online (Sandbox Code Playgroud)

然后我还创建了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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

是一个工作ASPNET页面,在文本编辑器创建.将它放入IIS虚拟目录,它正在工作.

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

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

Che*_*eso 20

好吧,我检查了Walther的教程,并运行了一个基本的MVC网站.

所需文件是:

Global.asax
App_Code\Global.asax.cs
App_Code\Controller.cs
Views\HelloWorld\Sample.aspx
web.config
Run Code Online (Sandbox Code Playgroud)

而已.

在Global.asax中,我提供了这个样板:

<%@ Application Inherits="MvcApplication1.MvcApplication" Language="C#" %>
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • http://server/root/HelloWorld(默认"动作")
  • http://server/root/HelloWorld/Index(与上面相同)
  • http://server/root/HelloWorld /英文
  • 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"
 %>
Run Code Online (Sandbox Code Playgroud)

而已!将上述内容放入IIS vdir可以获得一个有效的ASPNET MVC站点.

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

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