nopCommerce 3.9 大波浪系列 之 IWebHelper

接口:Nop.Core.IWebHelper

实现:Nop.Core.WebHelper

测试:Nop.Core.Tests.WebHelperTests

简介:Web辅助类

功能:获取客户端IP地址、当前请求Url、服务器变量、主机地址、URL参数值

        判断当前是否是安全连接、请求目标是否是静态资源、是否是重定向

        修改/删除 URL参数值、重启应用程序

  1 using System.Web;
  2 
  3 namespace Nop.Core
  4 {
  5     /// <summary>
  6     /// Represents a common helper
  7     /// </summary>
  8     public partial interface IWebHelper
  9     {
 10         /// <summary>
 11         /// Get URL referrer
 12         /// 获取客户端上次请求的url,默认实现:Request.UrlReferrer.PathAndQuery
 13         /// </summary>
 14         /// <returns>URL referrer</returns>
 15         string GetUrlReferrer();
 16 
 17         /// <summary>
 18         /// 获取客户端请求IP地址
 19         /// </summary>
 20         /// <returns>URL referrer</returns>
 21         string GetCurrentIpAddress();
 22 
 23         /// <summary>
 24         /// 获取当前页Url
 25         /// </summary>
 26         /// <param name="includeQueryString">False则不包含Url中的查询参数</param>
 27         /// <returns>Page name</returns>
 28         string GetThisPageUrl(bool includeQueryString);
 29 
 30         /// <summary>
 31         /// 获取当前页Url
 32         /// </summary>
 33         /// <param name="includeQueryString">False则不包含Url中的查询参数</param>
 34         /// <param name="useSsl">True则获取SSL安全页面Https://xxx</param>
 35         /// <returns>Page name</returns>
 36         string GetThisPageUrl(bool includeQueryString, bool useSsl);
 37 
 38         /// <summary>
 39         /// 当前连接是否是安全的
 40         /// </summary>
 41         /// <returns>true - 安全, false - 不安全</returns>
 42         bool IsCurrentConnectionSecured();
 43 
 44         /// <summary>
 45         /// 根据服务器变量名称获取值 
 46         /// </summary>
 47         /// <param name="name">服务器变量名称 例如:"HTTP_HOST"</param>
 48         /// <returns>服务器变量值</returns>
 49         string ServerVariables(string name);
 50 
 51         /// <summary>
 52         /// 获取主机地址
 53         /// </summary>
 54         /// <param name="useSsl">Use SSL</param>
 55         /// <returns>主机地址</returns>
 56         string GetStoreHost(bool useSsl);
 57 
 58         /// <summary>
 59         ///获取主机地址 默认调用 GetStoreHost(bool useSsl)
 60         /// </summary>
 61         /// <returns>Store location</returns>
 62         string GetStoreLocation();
 63 
 64         /// <summary>
 65         /// 获取主机地址
 66         /// </summary>
 67         /// <param name="useSsl">Use SSL</param>
 68         /// <returns>Store location</returns>
 69         string GetStoreLocation(bool useSsl);
 70 
 71         /// <summary>
 72         /// Returns true if the requested resource is one of the typical resources that needn't be processed by the cms engine.
 73         /// 请求目标是静态资源文件
 74         /// VirtualPathUtility.GetExtension
 75         /// </summary>
 76         /// <param name="request">HTTP Request</param>
 77         /// <returns>True为请求目标是静态资源文件.</returns>
 78         /// <remarks>
 79         /// These are the file extensions considered to be static resources:
 80         /// .css
 81         ///	.gif
 82         /// .png 
 83         /// .jpg
 84         /// .jpeg
 85         /// .js
 86         /// .axd
 87         /// .ashx
 88         /// </remarks>
 89         bool IsStaticResource(HttpRequest request);
 90 
 91         /// <summary>
 92         /// Modifies query string
 93         /// 修改查询字符串
 94         /// </summary>
 95         /// <param name="url">Url to modify</param>
 96         /// <param name="queryStringModification">Query string modification</param>
 97         /// <param name="anchor">Anchor</param>
 98         /// <returns>New url</returns>
 99         string ModifyQueryString(string url, string queryStringModification, string anchor);
100 
101         /// <summary>
102         /// Url中移除指定的查询字符串
103         /// </summary>
104         /// <param name="url">Url to modify</param>
105         /// <param name="queryString">Query string to remove</param>
106         /// <returns>New url</returns>
107         string RemoveQueryString(string url, string queryString);
108 
109         /// <summary>
110         /// Url获取参数名称对应的值
111         /// </summary>
112         /// <typeparam name="T"></typeparam>
113         /// <param name="name">Parameter name</param>
114         /// <returns>Query string value</returns>
115         T QueryString<T>(string name);
116 
117         /// <summary>
118         /// 重启应用程序
119         /// 该方法在商城重启中会用到
120         /// </summary>
121         /// <param name="makeRedirect">A value indicating whether we should made redirection after restart</param>
122         /// <param name="redirectUrl">Redirect URL; empty string if you want to redirect to the current page URL</param>
123         void RestartAppDomain(bool makeRedirect = false, string redirectUrl = "");
124 
125         /// <summary>
126         /// Gets a value that indicates whether the client is being redirected to a new location
127         /// 获取指示客户端是否重定向到新位置的值。
128         /// 如果位置响应标头的值与当前位置不同,则为 true;否则为 false。
129         /// </summary>
130         bool IsRequestBeingRedirected { get; }
131 
132         /// <summary>
133         /// Gets or sets a value that indicates whether the client is being redirected to a new location using POST
134         /// Post请求时获取指示客户端是否重定向到新位置的值。
135         /// </summary>
136         bool IsPostBeingDone { get; set; }
137     }
138 }
139 

 

同学们可以在测试类“Nop.Core.Tests.WebHelperTests”进行测试进一步理解IWebHelper接口

  1 using System.Collections.Specialized;
  2 using System.Web;
  3 using Nop.Core.Fakes;
  4 using Nop.Tests;
  5 using NUnit.Framework;
  6 
  7 namespace Nop.Core.Tests
  8 {
  9     [TestFixture]
 10     public class WebHelperTests
 11     {
 12         private HttpContextBase _httpContext;
 13         private IWebHelper _webHelper;
 14 
 15         [Test]
 16         public void Can_get_serverVariables()
 17         {
 18             var serverVariables = new NameValueCollection();
 19             serverVariables.Add("Key1", "Value1");
 20             serverVariables.Add("Key2", "Value2");
 21             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
 22             _webHelper = new WebHelper(_httpContext);
 23             _webHelper.ServerVariables("Key1").ShouldEqual("Value1");
 24             _webHelper.ServerVariables("Key2").ShouldEqual("Value2");
 25             _webHelper.ServerVariables("Key3").ShouldEqual("");
 26         }
 27 
 28         [Test]
 29         public void Can_get_storeHost_without_ssl()
 30         {
 31             var serverVariables = new NameValueCollection();
 32             serverVariables.Add("HTTP_HOST", "www.example.com");
 33             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
 34             _webHelper = new WebHelper(_httpContext);
 35             _webHelper.GetStoreHost(false).ShouldEqual("http://www.example.com/");
 36         }
 37 
 38         [Test]
 39         public void Can_get_storeHost_with_ssl()
 40         {
 41             var serverVariables = new NameValueCollection();
 42             serverVariables.Add("HTTP_HOST", "www.example.com");
 43             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
 44             _webHelper = new WebHelper(_httpContext);
 45             _webHelper.GetStoreHost(true).ShouldEqual("https://www.example.com/");
 46         }
 47 
 48         [Test]
 49         public void Can_get_storeLocation_without_ssl()
 50         {
 51             var serverVariables = new NameValueCollection();
 52             serverVariables.Add("HTTP_HOST", "www.example.com");
 53             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
 54             _webHelper = new WebHelper(_httpContext);
 55             _webHelper.GetStoreLocation(false).ShouldEqual("http://www.example.com/");
 56         }
 57 
 58         [Test]
 59         public void Can_get_storeLocation_with_ssl()
 60         {
 61             var serverVariables = new NameValueCollection();
 62             serverVariables.Add("HTTP_HOST", "www.example.com");
 63             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
 64             _webHelper = new WebHelper(_httpContext);
 65             _webHelper.GetStoreLocation(true).ShouldEqual("https://www.example.com/");
 66         }
 67 
 68         [Test]
 69         public void Can_get_storeLocation_in_virtual_directory()
 70         {
 71             var serverVariables = new NameValueCollection();
 72             serverVariables.Add("HTTP_HOST", "www.example.com");
 73             _httpContext = new FakeHttpContext("~/nopCommercepath", "GET", null, null, null, null, null, serverVariables);
 74             _webHelper = new WebHelper(_httpContext);
 75             _webHelper.GetStoreLocation(false).ShouldEqual("http://www.example.com/nopcommercepath/");
 76         }
 77 
 78         [Test]
 79         public void Get_storeLocation_should_return_lowerCased_result()
 80         {
 81             var serverVariables = new NameValueCollection();
 82             serverVariables.Add("HTTP_HOST", "www.Example.com");
 83             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
 84             _webHelper = new WebHelper(_httpContext);
 85             _webHelper.GetStoreLocation(false).ShouldEqual("http://www.example.com/");
 86         }
 87 
 88         [Test]
 89         public void Can_get_queryString()
 90         {
 91             var queryStringParams = new NameValueCollection();
 92             queryStringParams.Add("Key1", "Value1");
 93             queryStringParams.Add("Key2", "Value2");
 94             _httpContext = new FakeHttpContext("~/", "GET", null, null, queryStringParams, null, null, null);
 95             _webHelper = new WebHelper(_httpContext);
 96             _webHelper.QueryString<string>("Key1").ShouldEqual("Value1");
 97             _webHelper.QueryString<string>("Key2").ShouldEqual("Value2");
 98             _webHelper.QueryString<string>("Key3").ShouldEqual(null);
 99         }
100 
101         [Test]
102         public void Can_remove_queryString()
103         {
104             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
105             _webHelper = new WebHelper(_httpContext);
106             //first param (?)
107             _webHelper.RemoveQueryString("http://www.example.com/?param1=value1&param2=value2", "param1")
108                 .ShouldEqual("http://www.example.com/?param2=value2");
109             //second param (&)
110             _webHelper.RemoveQueryString("http://www.example.com/?param1=value1&param2=value2", "param2")
111                 .ShouldEqual("http://www.example.com/?param1=value1");
112             //non-existing param
113             _webHelper.RemoveQueryString("http://www.example.com/?param1=value1&param2=value2", "param3")
114                 .ShouldEqual("http://www.example.com/?param1=value1&param2=value2");
115         }
116 
117         [Test]
118         public void Can_remove_queryString_should_return_lowerCased_result()
119         {
120             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
121             _webHelper = new WebHelper(_httpContext);
122             _webHelper.RemoveQueryString("htTp://www.eXAmple.com/?param1=value1&parAm2=value2", "paRAm1")
123                 .ShouldEqual("http://www.example.com/?param2=value2");
124         }
125 
126         [Test]
127         public void Can_remove_queryString_should_ignore_input_parameter_case()
128         {
129             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
130             _webHelper = new WebHelper(_httpContext);
131             _webHelper.RemoveQueryString("http://www.example.com/?param1=value1&parAm2=value2", "paRAm1")
132                 .ShouldEqual("http://www.example.com/?param2=value2");
133         }
134 
135         [Test]
136         public void Can_modify_queryString()
137         {
138             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
139             _webHelper = new WebHelper(_httpContext);
140             //first param (?)
141             _webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param1=value3", null)
142                 .ShouldEqual("http://www.example.com/?param1=value3&param2=value2");
143             //second param (&)
144             _webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param2=value3", null)
145                 .ShouldEqual("http://www.example.com/?param1=value1&param2=value3");
146             //non-existing param
147             _webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param3=value3", null)
148                 .ShouldEqual("http://www.example.com/?param1=value1&param2=value2&param3=value3");
149         }
150 
151         [Test]
152         public void Can_modify_queryString_with_anchor()
153         {
154             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
155             _webHelper = new WebHelper(_httpContext);
156             _webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param1=value3", "Test")
157                 .ShouldEqual("http://www.example.com/?param1=value3&param2=value2#test");
158         }
159 
160         [Test]
161         public void Can_modify_queryString_new_anchor_should_remove_previous_one()
162         {
163             _httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
164             _webHelper = new WebHelper(_httpContext);
165             _webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2#test1", "param1=value3", "Test2")
166                 .ShouldEqual("http://www.example.com/?param1=value3&param2=value2#test2");
167         }
168     }
169 }
170 
Nop.Core.Tests.WebHelperTests


本文地址:http://www.cnblogs.com/yaoshangjin/p/nopCommerce39.html 

本文为大波浪原创、转载请注明出处。

如果您认为这篇文章还不错或者有所收获,可以点击下方的【关注】按钮,因为你的支持是我继续写作,分享的最大动力!
作者:大波浪
声明: 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果您发现博客中出现了错误,或者有更好的建议、想法,请及时与我联系!!如果想找我私下交流,可以私信或者加我QQ。
posted @ 2017-07-21 13:13  大波浪  阅读(515)  评论(0编辑  收藏  举报