ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [ASP.NET MVC 中Action要求与View本质

[ASP.NET MVC 中Action要求与View本质

原创 Linux操作系统 作者:iDotNetSpace 时间:2010-09-09 13:11:27 0 删除 编辑

创建一个自定义action必须满足的要求

 

方法必须为公共的.
方法不能为静态方法.
方法不能为扩展方法.
方法不能为一个构造器( getter,  setter).
方法不能包含ref 或 out 参数.

 

使用 NonActionAttribute 特性将阻止该action被调用

namespace MVCViewAndAction.Web.Controllers
{
    [HandleError]
    
public class UserDemoController : Controller
    {
        
//
        
// GET: /UserDemo/
        
//自定义一个简单的方法
        [NonAction]  
        
//public sealed class NonActionAttribute表示一个特性,该特性用于指示控制器方法不是操作方法。
        public string DisplayString()
        {
            
return "this is a demo string!";
        }
     }
}

 

理解Views

 

相对于 ASP.NET 与 Active Server Pages, ASP.NET MVC 并不包含任何直接对应的一个页面。在ASP.NET MVC 应用程序中,你键入浏览器地址栏中的URL在磁盘上并没有相应的一个页面,该URL被映射为 controller actions。与页面page最相近的正是我们所说的View。

 

最基本的如下:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> public ActionResult Index()
 {
      
return View();
 }

 

 

为了探究view的本质,以显示如下的结果:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> public class UserDemoController : Controller
    {
        
public RssActionResult RssShow()
        {
            
return new RssActionResult();
        }
    }

 

 

我们需要创建一个继承ActionResult的RssActionResult类,如下所示:

 

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->namespace MVCViewAndAction.Common
{
    
/*
     * System.Web.Mvc
    public abstract class ActionResult
    {
        protected ActionResult();
        public abstract void ExecuteResult(ControllerContext context);
    }
*/
    
public class RssActionResult:ActionResult
    {
        
public RssActionResult()
        { 
        }
        
//
        public override void ExecuteResult(ControllerContext context)
        {
            
if (context == null)
            {
                
throw new ArgumentNullException("ControllerContext is null!");
            }
            HttpResponseBase response 
= context.HttpContext.Response;
            Rss rss 
= new Rss();
            rss.CreateSampleRss(response);
        }
    }

 

 

通过从 System.Web.Mvc.ActionResult 类继承的自定义类型,ExecuteResult(ControllerContext context)启用对操作方法结果的处理。

 

附:ExecuteResult()中的rss.CreateSampleRss(response)方法通过利用HttpResponseBase来处理响应流。

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public void CreateSampleRss(HttpResponseBase response)
        {
            XmlTextWriter writer 
= new XmlTextWriter(response.OutputStream, System.Text.Encoding.UTF8); 
            WriteRssHeader(writer);
            
for (int i = 0; i < 50;i++ ) 
            {
                WriteRssItem(writer, 
"demo title: jasenkin" + i.ToString(), http://jasenkin/, "decription: --->" + i.ToString());
            }
            WriteRssBottom(writer);
            writer.Flush();
            writer.Close();
            response.ContentEncoding 
= System.Text.Encoding.UTF8;
            response.ContentType 
= "text/xml";
            response.Cache.SetCacheability(HttpCacheability.Public);
            response.End();
        }

 

 

 执行结果如下:

 

一个典型的Action可能接收用户输入,准备合适的响应数据, 然后通过返回一个Result的类型(如上例中的RssActionResult),系统将自动调用这个Result类型(如上例中的RssActionResult)的ExecuteResult(context)来响应浏览器请求,呈现的就是我们所说的View了(如上图)。

这仅仅是一个继承自actionresult的自定义类,其中的ExecuteResult()方法才是该类的关键之处。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-673040/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766002