这个问题可能听起来很奇怪,但我发现它真的难以消化ASP.NET MVC应用程序中的典型aspx页面。例如:ASP.NET MVC应用程序的aspx文件中的代码如何执行?
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
About Us
</asp:Content>
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>About</h2>
<p>
<% var cookie = Request.Cookies["mvcData"];
string lastRefreshValue = cookie == null? "Unknown": cookie["lastRefresh"];
%>
<%= Html.Encode ("Last Refreshed: " + lastRefreshValue) %>
<% var newCookie = new HttpCookie ("mvcData");
newCookie["lastRefresh"] = DateTime.Now.ToString();
newCookie.Expires = DateTime.Now.AddSeconds (10);
Response.Cookies.Add (newCookie);
%>
<%= Html.Encode (ViewState["hello"] ?? "") %>
</p>
</asp:Content>
写在<%的代码...%>运作良好,并预期,但我怎么能在一个类可视化代码?当然,它是从ViewPage继承而来的,它是一个类,所以无论代码写在<%..%>是否从某种方法执行。对?
编辑:编写在<%..%>中的代码是不相关。我写它来测试cookie。关键是这个代码如何映射到一个类中?
有很多关于MVC如何工作的东西,我认为你可能希望你有一个页面后面的代码,但你不需要它MVC。我也经历了这一点,这是值得的努力:) – 2009-07-23 12:43:20