asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用ASP.NET MVC实现站点而不使用Visual Studio?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我看过 ASP.NET MVC Without Visual Studio
是否可以生成基于ASP.NET MVC的网站,而不使用Visual studio?

而接受的答案是,是的。

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

这是一个类比。如果我想创建一个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虚拟目录中,它正在运行。

在文本编辑器中做什么,做一个基本的,你好的世界ASPNET MVC应用程序? (不带Visual studio)

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

解决方法

好的,我检查了 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#" %>
@H_820_3@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类中的每个公共方法都是一个动作;当该方法名称包含在url路径中的另一个段中时,将调用该方法。因此,对于上述控制器,这些URL将导致调用各种方法:

> http:// server / root / HelloWorld(默认的“action”)
> 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,图像和任何我喜欢的。

大佬总结

以上是大佬教程为你收集整理的如何使用ASP.NET MVC实现站点而不使用Visual Studio?全部内容,希望文章能够帮你解决如何使用ASP.NET MVC实现站点而不使用Visual Studio?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。