创建一个自定义action必须满足的要求
方法必须为公共的.
方法不能为静态方法.
方法不能是 Control基类中的方法(如:ToString,GetHashCode等)
方法不能为扩展方法.
方法不能为一个构 造函数 ,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。
最基本的如下:
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); } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索class
, 页面
, 方法
, context
, public
一个
intent.action view、action view、actionbar searchview、actionmenuview、actionbar自定义view,以便于您获取更多的相关知识。