学习Controller

其实学习了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种东西

  1. ViewResult – Represents HTML and markup.
  2. EmptyResult – Represents no result.
  3. RedirectResult – Represents a redirection to a new URL.
  4. JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  5. JavaScriptResult – Represents a JavaScript script.
  6. ContentResult – Represents a text result.
  7. FileContentResult – Represents a downloadable file (with the binary content).
  8. FilePathResult – Represents a downloadable file (with a path).
  9. 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.";
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值