其实学习了Controller了,觉得很简单,主要是以下几句话:
要发布到外面的要满足以下需求
In order to be exposed to the universe as an action, a method must meet certain requirements:
- The method must be public.
- The method cannot be a static method.
- The method cannot be an extension method.
- The method cannot be a constructor, getter, or setter.
- The method cannot have open generic types.
- The method is not a method of the controller base class.
- The method cannot contain ref or out parameters.
返回值,常见的view()其实是ViewResult
事实可以一共可以返回9种东西
- ViewResult – Represents HTML and markup.
- EmptyResult – Represents no result.
- RedirectResult – Represents a redirection to a new URL.
- JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
- JavaScriptResult – Represents a JavaScript script.
- ContentResult – Represents a text result.
- FileContentResult – Represents a downloadable file (with the binary content).
- FilePathResult – Represents a downloadable file (with a path).
- FileStreamResult – Represents a downloadable file (with a file stream).
而且一般来说不需要我们专门指定。如返回一个字符串什么的,只要直接这个string即可。
基本上什么都能返回了。
有一点:希望能发布出来使用,但是又不让它能直接在浏览器中打URL能访问到,可以加上 [NonAction]特性
using System.Web.Mvc; namespace MvcApplication1.Controllers { public class WorkController : Controller { [NonAction] public string CompanySecrets() { return "This information is secret."; } } }