使用 HttpRequest 对象HttpRequest 对象描述的是一个正在被处理的 HTTP 请求。下表列举了 HttpRequest 中的属性,它们提供了当前请求的相关信息(HttpRequest 类定义了一些方法和属性,我们会逐步讲解当中的一些属性)。表 1 &n ...
使用 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<get='_blank'>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 else23 {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>
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:[译] ASP.NET 生命周期 – ASP.NET 上下文对象(七)
关键词:ASP.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。