当前位置: 首页 > news >正文

做html的简单网站建筑工程信息频道

做html的简单网站,建筑工程信息频道,义乌网站建设成都网站设计,建设银行网站注册用户名本系列文章基于ASP.NET MVC Preview5. Controller是MVC中比较重要的一部分。几乎所有的业务逻辑都是在这里进行处理的#xff0c;并且从Model中取出数据。在ASP.NET MVC Preview5中#xff0c;将原来的Controller类一分为二#xff0c;分为了Controller类和ControllerBase类…本系列文章基于ASP.NET MVC Preview5. Controller是MVC中比较重要的一部分。几乎所有的业务逻辑都是在这里进行处理的并且从Model中取出数据。在ASP.NET MVC Preview5中将原来的Controller类一分为二分为了Controller类和ControllerBase类。Controller类继承自ControllerBase类而ControllerBase实现是了IController接口。 ControllerBase实现了IController接口的Execute方法在Route匹配到Controller之后就会调用Execute方法来进入Controller的处理。这里还定义了一个抽象的方法ExecuteCore方法该方法会在Execute方法的最后被调用。ControllerBase还定义了三个核心的属性。我们在后面会详细讨论TempData和ViewData。 Controller类除了继承自ControllerBase类以外还实现了好几个Filter接口Filter我们在后面再详细讨论。 public abstract class Controller : ControllerBase, IActionFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter{ }Controller类还定义很多有用的方法我们新建的Controller都必须继承自这个Controller类。例如我们新建一个AdminController public class AdminController : Controller { }Action方法 下面谈一下在Controller中比较重要的Action方法。在ASP.NET MVC中URL都是映射到Controller中的某个Action中然后由匹配的Action来处理我们的业务逻辑并返回view的。 Controller中的public的方法都被当作是Action方法。Action方法通常返回一个ActionResult的结果。例如我们为前面的AdminController定义一个Setting的Action方法用于设置Blog的一些基本参数 public class AdminController : Controller {     public ActionResult Setting()     {         throw new NotImplementedException();     } } 默认情况下Action方法的方法名就是这个Action的Action名(Action名指的是Route中匹配Action方法的URL的那部分。例如urlHome/Index其中Index就是Action名)。这里为什么要提到这个Action名呢应为Action名是可以定义的使用ActionNameAttribute来定义。请看下面的示例 public ActionResult Setting() {     throw new NotImplementedException(); }  [ActionName(Setting)]public ActionResult SaveSetting() {     throw new NotImplementedException(); } 这两个Action方法的Action名都为Setting即对于urlAdmin/Setting ,能同时匹配到这两个Action方法。如果一个URL同时匹配到两个Action方法的话程序会抛出一个错误 如果我们希望这两个Action的Action名都为SettingSetting()就用于显示一个表单页面给用户而SaveSetting()就用于保存用户提交过来的表单数据我们该怎么做呢我们可以利用AcceptVerbsAttribute来设置这个Attribute用来定义Action方法会匹配指定的HttpMethod。例如下面的代码 [AcceptVerbs(GET)]public ActionResult Setting() {     throw new NotImplementedException(); }  [ActionName(Setting), AcceptVerbs(POST)]public ActionResult SaveSetting() {     throw new NotImplementedException(); } 这样对于HttpMethod为GET的客户端请求就会匹配到Setting()来显示一个表单给用户如果用户POST回来的表单数据则会匹配到SaveSetting()上面去我们就可以处理用户POST过来的数据并保存到数据库。 在这里AcceptVerbsAttribute是继承自ActionSelectionAttribute的我们也可以继承自ActionSelectionAttribute来自定义自己想要实现的功能。这个我们后面会详细讲解。如果你比较心急可以看下Asp.net Mvc Preview 5 体验--实现ActionSelectionAttribute来判断是否为AJAX请求而选择不同的Action这篇文章。 如果你想将一个public的方法设置为不是Action方法那么你就要为该public的方法添加NonAction的Attribute Action方法的参数 例如我们要在AdminController中定义一个编辑日志的Action方法 public ActionResult EditPost(int? id) {     throw new NotImplementedException(); }对于URLAdmin/EditPost/2 上面的参数会自动被赋值为2。ASP.NET MVC在匹配Route的时候会根据Route的设置自动为Action方法的参数赋值。所以前面的id参数会被自动赋值为2的前提是在Route配置的时候必须指定了id参数例如 routes.MapRoute(     Default,                                              // Route 的名称     {controller}/{action}/{id},                           // 带有参数的URL     new { controller  Home, action  Index, id   }  // 设置默认的参数);如果我们将Route修改为 routes.MapRoute(     Default,                                              // Route 的名称     {controller}/{action}/{para},                           // 带有参数的URL     new { controller  Home, action  Index, para   }  // 设置默认的参数);则前面的Action方法的参数必须修改为public ActionResult EditPost(int? para){ }使Action方法的参数和Route中定义的参数名相同ASP.NET MVC才能自动为Action方法的参数赋值。 ActionResult Action方法返回ActionResult类型的结果。ASP.NET MVC为我们提供了几种ActionResult的实现如下 ViewResult. 呈现视图页给客户端。由View 方法返回. RedirectToRouteResult. 重定向到另外一个Route。由RedirectToAction 和RedirectToRoute 方法返回. RedirectResult. 重定向到另外一个URL。由 Redirect 方法返回. ContentResult. 返回普通的内容。例如一段字符串。由 Content 方法返回. JsonResult. 返回JSON结果。由 Json 方法返回. EmptyResult. 如果Action必须返回空值可以返回这个结果。Controller中没有实现的方法可以return new EmptyResult();. 当然我们也可以自定一个我们的ActionResult返回给客户端例如一个RssResult。可以参考Asp.Net MVC实践 - 自定义ActionResult实现Rss输出 (基于ASP.NET MVC Preview 3)这篇文章。 通常情况下我们的Controller可能有一些相同的情况例如我们在各个Controller中都有可能会在出错或者什么时候想要显示一条提示信息给用户或者有一些共同的数据要呈现的。这时候我们最好就定义一个我们自己的Controller的基类 public class BaseController : Controller {     public BaseController()     {      }      protected ActionResult ShowMsg(Liststring msgs)     {         throw new NotImplementedException();     }      public ActionResult Message()     {         throw new NotImplementedException();     } } 然后其他的Controller都继承自这个BaseController public class AdminController : BaseController {     [AcceptVerbs(GET)]     public ActionResult Setting()     {         throw new NotImplementedException();     }      [ActionName(Setting), AcceptVerbs(POST)]     public ActionResult SaveSetting()     {         throw new NotImplementedException();     }      public ActionResult EditPost(int? id)     {         throw new NotImplementedException();     } } 好时间不早了就先到这里吧。EnjoyPost by Q.Lee.lulu。
http://wiki.neutronadmin.com/news/39812/

相关文章:

  • 同济大学 土木工程毕业设计 久久建筑网seo专员很难吗
  • 上海网站制作公司报价网站建设程序结构
  • 网站模板免费下载php有什么免费的wordpress
  • 优才网站建设简单网页制作html
  • 建设企业高端网站专业商城网站设计
  • 东莞非凡网站建设网站制作的相关术语
  • 做公司+网站建设价格网站 使用的字体
  • 邯郸网站设计哪家好建设银行网站理财产品为何不让买
  • 接帮人家做网站的网站濮阳网站建设电话
  • 深圳网站设计 三把火科技注册安全工程师有什么用
  • 网站上传文件夹软件定制图片
  • 网站系统繁忙是什么意思临沂网站建设培训
  • 网站页脚设计一个学校怎么制作网站
  • 西安公司网站制作价格网站建设全程揭秘 课件下载
  • 墙膜 东莞网站建设客户网站留言
  • 手机 网站 模板哈尔滨做公司网站的公司有哪些
  • 互动型网站做网站销售一个星期的计划
  • 网站建设相关制度南京前十名传媒广告公司
  • 商城网站建设多少钱网站文章更新频率
  • 自建站推广方式php伪静态网站破解
  • 唐山哪个公司做网站wordpress 5.0.1编辑器
  • 淄博桓台网站建设报价府网站建设运维情况自查报告
  • 电商网站建设源码巩义网络建设网站
  • 公司网站备案条件专业制作网站服务
  • 合网站建设网站收录怎么弄
  • 清远专业网站制作公司唐山做网站优化公司
  • 做网站图片格式北京网页设计师工资有多少
  • 服装设计网站大全免费推广平台有哪些 请一一例举
  • 北京个人网站设计app软件免费下载安装最新版
  • 网站建设 百度云长春中企动力怎么样