zoukankan      html  css  js  c++  java
  • [译] ASP.NET 生命周期 – ASP.NET 上下文对象(七)

    使用 HttpRequest 对象

    HttpRequest 对象描述的是一个正在被处理的 HTTP 请求。下表列举了 HttpRequest 中的属性,它们提供了当前请求的相关信息(HttpRequest 类定义了一些方法和属性,我们会逐步讲解当中的一些属性)。

    表 1 – HttpRequest 类中属性

    名称 描述
    AcceptTypes 返回一个可以被浏览器接受的 MIME 类型的字符串数组。
    Browser 返回一个可以用来描述浏览器功能的 HttpBrowserCapabilities 对象。
    ContentEncoding 返回一个 System.Text.Encoding 对象,用来描述对请求数据进行编码的字符集。
    ContentLength 返回请求内容的字节数。
    ContentType 返回请求中内容的 MIME 类型。
    CurrentExecutionFilePathExtension 返回请求的 URL 文件扩展组件。
    Headers 返回一个包含请求头的集合。
    HttpMethod 返回发起请求的 HTTP 方法(GET, POST, 等等)。
    InputStream 返回一个可以读取请求内容的流。
    IsLocal 如果请求源自本机,那么返回 true。
    MapPath(path) 将项目中的文件名转换成绝对路径。
    RawUrl 返回紧跟着主机名的那部分 URL。换句话说,比如,http://apress.com:80/books/Default.aspx,那么这个属性就会返回 /books/Default.aspx。
    RequestContext 返回一个请求上下文对象用来提供获取一个请求的路由信息。
    Url 返回一个 System.Uri 对象用来表示请求 URL。
    UrlReferrer 返回一个 System.Uri 对象用来表示访问来源 URL。
    UserAgent 返回浏览器提供的 user-agent 字符串。
    UserHostAddress 返回远程客户端的 IP 地址,用一个字符串表示。
    UserHostName 返回远程客户端的 DNS 名称。
    UserLanguages 返回一个字符串数组表示浏览器/用户偏好的语言。

    为了阐述 HttpRequest 类的使用,我已经修改了 Index.cshtml 文件,用来显示一些请求属性。

     1 @using SimpleApp.Models
     2 @model List<string>
     3 
     4 @{
     5     Layout = null;
     6 }
     7 
     8 <!DOCTYPE html>
     9 
    10 <html>
    11 <head>
    12     <meta name="viewport" content="width=device-width" />
    13     <title>Vote</title>
    14     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    15 </head>
    16 <body class="container">
    17     <div class="panel panel-primary">
    18         @if (ViewBag.SelectedColor == null)
    19         {
    20             <h4 class="panel-heading">Vote for your favourite color</h4>
    21         }
    22         else
    23         {
    24             <h4 class="panel-heading">Change your vote from @ViewBag.SelectedColor</h4>
    25         }
    26 
    27         <div class="panel-body">
    28             @using (Html.BeginForm())
    29             {
    30                 @Html.DropDownList("color", new SelectList(Enum.GetValues(typeof(Color))), "Change a Color", new { @class = "form-control" })
    31 
    32                 <div>
    33                     <button class="btn btn-primary center-block" type="submit">Vote</button>
    34                 </div>
    35             }
    36         </div>
    37     </div>
    38 
    39     <div class="panel panel-primary">
    40         <h5 class="panel-heading">Results</h5>
    41         <table class="table table-condensed table-striped">
    42             <tr><th>Color</th><th>Votes</th></tr>
    43             @foreach (Color c in Enum.GetValues(typeof(Color)))
    44             {
    45                 <tr>
    46                     <td>@c</td>
    47                     <td>@Votes.GetVotes(c)</td>
    48                 </tr>
    49             }
    50         </table>
    51     </div>
    52 
    53     <div class="panel panel-primary">
    54         <h5 class="panel-heading">Request Properties</h5>
    55         <table class="table table-condensed table-striped">
    56             <tr><th>Property</th><th>Value</th></tr>
    57             <tr><td>HttpMethod</td><td>@Request.HttpMethod</td></tr>
    58             <tr><td>IsLocal</td><td>@Request.IsLocal</td></tr>
    59             <tr><td>RawURL</td><td>@Request.RawUrl</td></tr>
    60         </table>
    61     </div>
    62 </body>
    63 </html>
    View Code

    HttpRequest 对象使用得很频繁以致一些应用组件,包括 Razor 视图,都提供了方便的属性,这样我们就不需要为了获取一个 HttpRequest 实例而动用 HttpContext 对象。下表总结了可以获取到 HttpRequest 对象的方便属性:

    表 2 - 在不同的 ASP.NET/MVC 组件中获取一个 HttpRequest 对象

    组件 技术
    Controller 使用方便的 Request 属性。
    View 使用方便的 Request 属性。
    全局应用类 使用方便的 Request 属性。
    模块 没有方便的属性可用。使用 HttpContext.Request 属性。
    处理器 没有方便的属性可用。使用 HttpContext.Request 属性。
    全局 总是可以通过静态的 HttpContext.Current.Request 属性获取到 HttpRequest 对象。

    3.8 Displaying details of the request(1)3.8 Displaying details of the request(2)

    图 1 - 展示请求的详细信息

    除了表 1 中提到的属性,一个请求中还包括了其他的属性来获取数据。我在下表列举了出来,但是因为模型绑定的缘故,它们不直接在 MVC controllers 中使用,这在 Pro ASP.NET MVC 5 中讲到过。然而,这些属性也有时候在模块中使用到。

    表 3 – HttpRequest 类中定义的额外属性

    名称 描述
    Files 返回一个浏览器表单中发送的文件集合。
    Form 提供对原始表单数据的访问。
    Params 一个来自查询字符串,表单字段,和 cookies 的组合数据项集合。也可以直接在 HttpRequest 对象上使用一个类数组的索引,比如 Request[“myname”] 和 Request.Params[“myname”] 是等同的。
    QueryString 返回一个查询字符串参数的集合;这个属性通常不直接在 MVC 应用中使用。

    [根据 Adam Freeman – Pro ASP.NET MVC 5 Platform 选译]

  • 相关阅读:
    赶集个人 车源推送到 58
    fsockopen get,post 封装 (转)
    php write excel
    《HTTP协议详解》读书笔记---请求篇之消息报头
    《HTTP协议详解》读书笔记---请求篇之响应状态码
    《HTTP协议详解》读书笔记---请求篇之情求方法
    高效使用 GNOME Files(Nautilus) 管理文件
    手机无法连接 GSConnect:防火墙未开放端口
    Ubuntu 上安装 ArtiPub(一款开源的一文多发平台)
    GNOME 桌面必备扩展(GNOME Shell Extensions)
  • 原文地址:https://www.cnblogs.com/levid-gc/p/5198588.html
Copyright © 2011-2022 走看看