Asp.net mvc 2 in action 笔记 -2 View Controller

简介: View 第3、10章 ViewData 和强类型视图          一般情况下,定义presentation model,然后形成强类型视图,可以结合ViewData传递小的片段和简单数据,以增加代码的维护性          presentation model类中可以加入data ...

View

第3、10章

ViewData 和强类型视图

         一般情况下,定义presentation model,然后形成强类型视图,可以结合ViewData传递小的片段和简单数据,以增加代码的维护性

         presentation model类中可以加入data annotations属性,控制数据的验证

HTML helper类

DisplayFor

DisplayTextFor

EditorFor

CheckBoxFor

DropDownListFor

HiddenFor

LabelFor

ListBoxFor

PasswordFor

RadioButtonFor

TextAreaFor

TextBoxFor

ValidateFor

ValidationMessageFor

以上类都在 System.Web.Mvc ..::. HtmlHelper 类中,MSDN文档可以察看明细,目前这些代码自能手工编写或使用T4自动生成,我估计以后IDE会提供类似WebForm的可视化支持

强类型模板

根据察看和编辑的不同,分为两类

■ Html.Display("Message")

■ Html.DisplayFor(m => m.Message)

■ Html.DisplayForModel()

■ Html.Editor("UserName")

■ Html.EditorFor(m => m.UserName)

■ Html.EditorForModel()

模板查找顺序和自定义

If the template helper method is used inside an area-specific view, these folders include

■ <Area>/<ControllerName>/EditorTemplates/<TemplateName>.ascx (or .aspx)

■ <Area>/Shared/EditorTemplates/<TemplateName>.ascx (or .aspx)

If a template isn’t found in these folders, or if the view isn’t in an area, the default view search locations are used:

■ <ControllerName>/EditorTemplates/<TemplateName>.ascx (or .aspx)

■ Shared/EditorTemplates/<TemplateName>.ascx (or .aspx)

对于察看模板在DisplayTemplates下[第20章有一个使用这个的例子]

Master pages

和Web Form基本类似

Partials

RenderPartial method or the Partial method in a parent view

对于RenderPartial中视图名称在Views目录下的查找顺序:

1 <Area>\<Controller>\<PartialName>.aspx and .ascx

2 <Area>\Shared\<PartialName>.aspx and .ascx

3 \<Controller>\<PartialName>.aspx and .ascx

4 \Shared\<PartialName>.aspx and .ascx

Building query-string parameter lists

处理RouteValueDictionary,使URL象以前的格式如:***?##=##

在MVC中使用Router基本上这种形式不再使用,除非和已有的一些老系统兼容处理可能用到

View Engine

默认是Web Form视图引擎

开源的Spark引擎,具体使用需要遵守这个的语法即可

在MVC 3中已经有Razor视图引擎,微软PI一体化简装包中安装后默认建立的页面使用的就是Razor,毕竟是新加的,没有历史包袱,比Web Form简化

Controller

第4、9、16章

所有的请求都通过这个进行服务

Post-Redirect-Get (PRG)

用于防止表单提交的问题,如果在同一个视图中展现提交结果,客户端刷新时会再次提交从而造成数据的重复和不一致

[HttpPost]

public ActionResult Edit(UserInput input)                   

{

    if (ModelState.IsValid)         

    {

        UpdateUserFromInput(input);                         

        TempData["message"] = "The user was updated";       

        return RedirectToAction("index");          

    }

    return View(input);               

}

private void UpdateUserFromInput(UserInput input)           

{                                                           

    User user =  UserRepository.GetByUsername(input.Username);       

    user.FirstName = input.FirstName;                       

    user.LastName = input.LastName;                         

    UserRepository.Save(user);                              

}

ActionFilter

This extensibility point allows you to intercept the execution of an action and inject behavior before or after the action is executed. This is similar to aspect-oriented programming, which is a technique for applying cross-cutting concerns to a code base without having lots of duplicate code to maintain.

自己实现只要从ActionFilterAttribute继承即可,如记录日志等

ChildActionOnlyAttribute是框架内置的,比较这个属性的Action,只能在Html.RenderAction的视图中使用

AuthorizeAttribute

Action selectors

The action selector is used to control which action method is selected to handle a particular route.

System.Web.Mvc ..::. ActionMethodSelectorAttribute

       System.Web.Mvc ..::. AcceptVerbsAttribute

       System.Web.Mvc ..::. HttpDeleteAttribute

       System.Web.Mvc ..::. HttpGetAttribute

       System.Web.Mvc ..::. HttpPostAttribute

       System.Web.Mvc ..::. HttpPutAttribute

       System.Web.Mvc ..::. NonActionAttribute

Action results

Custom action results can be used to remove code that’s duplicated across methods and to extract dependencies that can make an action difficult to test.

直接继承ActionResult类

System.Web.Mvc ..::. ActionResult 
     System.Web.Mvc ..::. ContentResult 
     System.Web.Mvc ..::. EmptyResult 
     System.Web.Mvc ..::. FileResult  FileContentResult

     System.Web.Mvc ..::. HttpUnauthorizedResult

     System.Web.Mvc ..::. JavaScriptResult

     System.Web.Mvc ..::. JsonResult

     System.Web.Mvc ..::. RedirectResult

     System.Web.Mvc ..::. RedirectToRouteResult

     System.Web.Mvc ..::. ViewResultBase

直接实现一个ActionReslut可以共用,如结果输出CSV文件。

       public class CsvActionResult : ActionResult 
       {
              public IEnumerable ModelListing { get; set; }
 
              public CsvActionResult(IEnumerable modelListing)
              {
                    ModelListing = modelListing;
              }
 
              public override void ExecuteResult(ControllerContext context) 
              {
                    byte[] data = new CsvFileCreator().AsBytes(ModelListing);
                    
                    var fileResult = new FileContentResult(data, "text/csv") 
                    {
                           FileDownloadName = "CsvFile.csv"
                    };
 
                    fileResult.ExecuteResult(context);
              }
       }

主要是在Action方法的返回时实例化以上不同的实现即可。

Routing

URLRewriting

路由注册在Global.asax.cs完成

      public static void RegisterRoutes(RouteCollection routes)
        {
            /*  Desired URL schema:
             * 1.  example.com/                     home page
             * 2.  example.com/privacy              static page containing privacy policy
             * 3.  example.com/widgets              show a list of the widgets
             * 4.  example.com/<widget code>           Shows a product detail page for the relevant <widget code>
             * 5.  example.com/<widget code>/buy       Add the relevant widget to the shopping basket
             * 6.  example.com/basket                   Shows the current users shopping basket
             * 7.  example.com/checkout                   Starts the checkout process for the current user
             * 8.  example.com/404                  show a friendly 404 page
             */
 
 
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            routes.MapRoute("404", "404", new {controller = "Error", action = "NotFound"});
 
            routes.MapRoute("privacy_policy", "privacy", new { controller = "Home", action = "Privacy" });
 
            routes.MapRoute("widget", "{widgetCode}/{action}",
                            new { controller = "Catalog", action = "Show" },
                            new { widgetCode = @"WDG-\d{4}" });
 
            routes.MapRoute("widgets", "widgets", new {controller = "Catalog", action = "index"});
 
            routes.MapRoute("catalog", "{action}",
                            new { controller = "Catalog" },
                            new { action = @"basket|checkout|index" });
 
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
            );
 
            routes.MapRoute("404-catch-all", "{*catchall}", new {Controller = "Error", Action = "NotFound"});
        }

路由处理的优先级从上到下,因此这个的设计是关键

URL生成

Html.ActionLink

Html.RouteLink

Url.Action

以上的缺点是参数都是字符串的形式,如果Action换名后,需要手动修改

T4MVC是一个自动化的辅助工具处理URL的生成问题,变成强类型,编译时完成处理。

相关文章
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
41 0
|
1月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
95 5
|
3月前
|
XML 前端开发 应用服务中间件
Cannot resolve MVC View解决方案
Cannot resolve MVC View解决方案
120 0
Cannot resolve MVC View解决方案
|
3月前
|
前端开发 JavaScript Java
springboot 出现 Cannot resolve MVC View ‘index‘ 问题解决办法,前后端不分离项目前端文件存放位置,已经如何访问
springboot 出现 Cannot resolve MVC View ‘index‘ 问题解决办法,前后端不分离项目前端文件存放位置,已经如何访问
104 0
|
8月前
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
116 0
|
4月前
|
XML 前端开发 JavaScript
SpringMVC中单独配置<mvc:default-servlet-handler/> 导致 Controller失效
SpringMVC中单独配置<mvc:default-servlet-handler/> 导致 Controller失效
75 0
|
5月前
|
开发框架 自然语言处理 前端开发
基于ASP.NET MVC开发的、开源的个人博客系统
基于ASP.NET MVC开发的、开源的个人博客系统
52 0
|
8月前
|
SQL 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(完:内附源码)
经过一段时间的准备,【ASP.NET Core MVC开发实战之商城系统】已经完成,目前代码已开发完成,先将全部内容整理分享,如有不足之处,还请指正。
107 0
|
8月前
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(六)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情,购物车等功能的开发,今天继续讲解订单管理功能开发,仅供学习分享使用,如有不足之处,还请指正。
215 0
|
9月前
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
67 0