我怎样才能实现与ASP.NET MVC网站没有使用Visual Studio? [英] How can I implement a site with ASP.NET MVC without using Visual Studio?
问题描述
我见过 ASP.NET MVC没有Visual Studio的,该问,
的是否有可能产生基于ASP.NET MVC网站,不使用Visual Studio?的
和接受的答案是,的是的。
And the accepted answer is, yes.
好了,下一个问题:如何
下面是一个比喻。如果我想创建一个ASP.NET Web表单页面,我加载了我最喜欢的文本编辑器,创建一个文件名为的东西。 ASPX。然后,我插入到该文件,一些样板:
Here's an analogy. If I want to create an ASP.NET Webforms page, I load up my favorite text editor, create a file named Something.aspx. Then I insert into that file, some boilerplate:
<%@ 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文件:
Then I also create the Sourcefile.cs file:
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虚拟目录,它的工作。
And that is a working ASPNET page, created in a text editor. Drop it into an IIS virtual directory, and it's working.
我有什么做的,做一个基本的,的Hello,World ASPNET MVC应用程序,在文本编辑器? (没有的Visual Studio 的)
What do I have to do, to make a basic, hello, World ASPNET MVC app, in a text editor? (without Visual Studio)
假设我想用一个控制器,一个视图和一个简单的模型一个基本的MVC应用程序。我需要什么文件来创建,什么会去到他们?
Suppose I want a basic MVC app with a controller, one view, and a simple model. What files would I need to create, and what would go into them?
推荐答案
好吧,我检查<一href=\"http://www.asp.net/%28S%28pdfrohu0ajmwt445fanvj2r3%29%29/learn/mvc/tutorial-05-cs.aspx\">Walther's教程并得到了一个基本的MVC网站的运行。
ok, I examined Walther's tutorial and got a basic MVC site running.
所需的文件是:
Global.asax
App_Code\Global.asax.cs
App_Code\Controller.cs
Views\HelloWorld\Sample.aspx
web.config
就是这样。
在Global.asax里面,我提供这个样板:
Inside the Global.asax, I provide this boilerplate:
<%@ Application Inherits="MvcApplication1.MvcApplication" Language="C#" %>
和该 MvcApplication
类是一个叫的Global.asax.cs模块,它必须被放置到APP_ code目录中定义的。内容是这样的:
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);
}
}
该Controller.cs提供处理各种请求的逻辑。在这个简单的例子,控制器类是这样的:
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
}
}
}
控制器必须有一个名为 XxxxxController
,其中xxxxx的部分定义了URL路径段。对于一个名为 HelloWorldController
控制器,URL路径段的HelloWorld
。在控制器类的每个公共方法是一个的动作的;当被包含在URL路径中另一段的方法名称调用的方法。因此,对于上述的控制器,这些URL会导致调用的各种方法:
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:/ /服务器/根/ HelloWorld的(默认为行动)
- 的http:/ /服务器/根/的HelloWorld /指数(同上)
- 的http:/ /服务器/根/的HelloWorld /英文
- 的http:/ /服务器/根/的HelloWorld /意大利语
- 的http:/ /服务器/根/的HelloWorld /样品(一个视图,Sample.aspx实现)
每个方法返回的操作结果的,下列操作之一:查看(aspx页面),重定向,空,文件(各种选项),JSON,内容(任意文本),和Javascript
Each method returns an Action result, one of the following: View (aspx page), Redirect, Empty, File (various options), Json, Content (arbitrary text), and Javascript.
在View页面,如Sample.aspx在这种情况下,必须从 System.Web.Mvc.ViewPage
派生。
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"
%>
这就是它!删除上述内容到IIS VDIR给了我一个工作ASPNET MVC的网站。
That's it! Dropping the above content into an IIS vdir gives me a working ASPNET MVC site.
(嗯,我还需要在web.config文件,里面有配置的8K在它的所有这一切都源$ C $ c和配置可供浏览或下载。)
(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.)
然后我可以添加其他静态内容:JS,CSS,图片及其他任何我喜欢
And then I can add other static content: js, css, images and whatever else I like.
这篇关于我怎样才能实现与ASP.NET MVC网站没有使用Visual Studio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!