实现Controller的两种形式

形式1:仅仅实现IController接口,自定义Controller对Request的实现。
形式2:在实现IController接口以后,继承Controller的一个基类,利用MVC Framework提供的一些特性实现Controller。

1、通过实现IController接口创建controller:

public class BasicController:IController

{

public void Execute(RequestContextrequestContext)

{

string controller=(string)requestContext.RouteData.Values["controller"];

string action=(string)requestContext.RouteData.Values["action"];

requestContext.HttpContext.Response.Write(string.Format("Controller:{0},Action:{1}",controller,action));

}

}

实现接口的意义是告诉MVC Framewrok我们创建的这个类型是一个controller,但是Framework并没有规定该怎样处理Request,因此要我们自定义实现处理请求的方式。

2、通过继承Controller 基类来创建controller:

在实现IController以后要进行自定义可能会很麻烦,微软给我们提供了一些现成的特性,我们可以在这些特性上进行拓展来实现我们想要的形式。我们可以通过继承Controller的积累来获取这些特性,这些特性包括:

1、Action方法:一个Controller执行的动作会被分配到几个Action方法中,每个action方法都要通过不同的URL来请求,被请求的action会执行,他的参数来自请求中的参数。

2、Action Result:你可以用一个对象来说明你的 action 执行的结果,这样的对象可以是:一个View跳转到另外一个URL,或者Acton方法。

3、过滤器:将会被从用的东西封装到一起。

另外我们还要知道的是我们通过VS右键——Add——Controller得到的controller都是继承了Contrloller基类的,而这个也是最好的创建controller的方式。

示例代码:

public class DerivedController : Controller {
   public ActionResult Index() {
      ViewBag.Message = "Hello from the DerivedController Index method";
      return View("MyView");
   }
}

Controller@实现Controller的两种形式的更多相关文章

  1. C++:一般情况下,设计函数的形参只需要两种形式

    C++:一般情况下,设计函数的形参只需要两种形式.一,是引用形参,例如 void function (int &p_para):二,是常量引用形参,例如 void function(const ...

  2. jquery插件的两种形式

    这里总结一下jquery插件的两种形式,一种是通过字面量的形式组织代码,另一种是通过构造函数的方式.下面就两种形式来分析俩个例子. 例子1: ;(function ($,window,document ...

  3. SQL 关于apply的两种形式cross apply 和 outer apply(转)

    转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...

  4. SQL 关于apply的两种形式cross apply 和 outer apply

    SQL 关于apply的两种形式cross apply 和 outer apply 例子: CREATE TABLE [dbo].[Customers]( ) COLLATE Chinese_PRC_ ...

  5. SQL关于apply的两种形式cross apply和outer apply(转载)

    SQL 关于apply的两种形式cross apply 和 outer apply   apply有两种形式: cross apply 和 outer apply   先看看语法:   <lef ...

  6. 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编

    在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编 ...

  7. 在sql中case子句的两种形式

    case子句,在select后面可以进行逻辑判断. 两种形式:判断相等.判断不等 一.判断相等的语法: case 列名 when ...  then ... when ...  then ... el ...

  8. 转:SQL 关于apply的两种形式cross apply 和 outer apply

    原文地址:http://www.cnblogs.com/Leo_wl/archive/2013/04/02/2997012.html SQL 关于apply的两种形式cross apply 和 out ...

  9. SQL 关于apply的两种形式cross apply 和 outer apply, with cube 、with rollup 和 grouping

    1). apply有两种形式: cross apply 和 outer apply先看看语法: <left_table_expression> {cross|outer} apply &l ...

随机推荐

  1. java int and string convert

    int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=String.valueOf( ...

  2. Week15(12月16日):授课综述1

    Part I:提问 =========================== 1.(   )类提供了一个对Entity Framework的抽象,能够进行数据持久化并接受数据. A.Layout    ...

  3. 浏览器对body节点scrollTop解析的差异

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style t ...

  4. Windows Phone 8初学者开发—第6部分:设置应用程序的样式

    原文 Windows Phone 8初学者开发—第6部分:设置应用程序的样式 Source Code: http://aka.ms/absbeginnerdevwp8  PDF Version: ht ...

  5. python 模块BeautifulSoup使用

    BeautifulSoup是一个专门用于解析html/xml的库.官网:http://www.crummy.com/software/BeautifulSoup/ 说明,BS有了4.x的版本了.官方说 ...

  6. Ubuntu实现双网卡双IP双待机

    Ubuntu实现双网卡双IP双待机 待机是借用了手机中的说法,其实是电脑上有两个网卡,一个无线,一个有线的.要实现无线访问外网Google Baidu查资料,有线网卡直接连接开发板.在Ubuntu上配 ...

  7. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  8. HDU4648+Easy

    N^2都能过!!!!!!! /* Easy */ #include<stdio.h> #include<string.h> #include<stdlib.h> # ...

  9. 1294 - Positive Negative Sign(规律)

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  10. lib32gcc1 : Depends: gcc-4.9-base (= 4.9-20140406-0ubuntu1) but 4.9.3-0ubuntu4

    运行:sudo apt-get update 然后重新安装lib32gcc1