nopCommerce 3.9 大波浪系列 之 IWebHelper

接口:Nop.Core.IWebHelper

实现:Nop.Core.WebHelper

测试:Nop.Core.Tests.WebHelperTests

简介:Web辅助类

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

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

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

 using System.Web;

 namespace Nop.Core
{
/// <summary>
/// Represents a common helper
/// </summary>
public partial interface IWebHelper
{
/// <summary>
/// Get URL referrer
/// 获取客户端上次请求的url,默认实现:Request.UrlReferrer.PathAndQuery
/// </summary>
/// <returns>URL referrer</returns>
string GetUrlReferrer(); /// <summary>
/// 获取客户端请求IP地址
/// </summary>
/// <returns>URL referrer</returns>
string GetCurrentIpAddress(); /// <summary>
/// 获取当前页Url
/// </summary>
/// <param name="includeQueryString">False则不包含Url中的查询参数</param>
/// <returns>Page name</returns>
string GetThisPageUrl(bool includeQueryString); /// <summary>
/// 获取当前页Url
/// </summary>
/// <param name="includeQueryString">False则不包含Url中的查询参数</param>
/// <param name="useSsl">True则获取SSL安全页面Https://xxx</param>
/// <returns>Page name</returns>
string GetThisPageUrl(bool includeQueryString, bool useSsl); /// <summary>
/// 当前连接是否是安全的
/// </summary>
/// <returns>true - 安全, false - 不安全</returns>
bool IsCurrentConnectionSecured(); /// <summary>
/// 根据服务器变量名称获取值
/// </summary>
/// <param name="name">服务器变量名称 例如:"HTTP_HOST"</param>
/// <returns>服务器变量值</returns>
string ServerVariables(string name); /// <summary>
/// 获取主机地址
/// </summary>
/// <param name="useSsl">Use SSL</param>
/// <returns>主机地址</returns>
string GetStoreHost(bool useSsl); /// <summary>
///获取主机地址 默认调用 GetStoreHost(bool useSsl)
/// </summary>
/// <returns>Store location</returns>
string GetStoreLocation(); /// <summary>
/// 获取主机地址
/// </summary>
/// <param name="useSsl">Use SSL</param>
/// <returns>Store location</returns>
string GetStoreLocation(bool useSsl); /// <summary>
/// Returns true if the requested resource is one of the typical resources that needn't be processed by the cms engine.
/// 请求目标是静态资源文件
/// VirtualPathUtility.GetExtension
/// </summary>
/// <param name="request">HTTP Request</param>
/// <returns>True为请求目标是静态资源文件.</returns>
/// <remarks>
/// These are the file extensions considered to be static resources:
/// .css
/// .gif
/// .png
/// .jpg
/// .jpeg
/// .js
/// .axd
/// .ashx
/// </remarks>
bool IsStaticResource(HttpRequest request); /// <summary>
/// Modifies query string
/// 修改查询字符串
/// </summary>
/// <param name="url">Url to modify</param>
/// <param name="queryStringModification">Query string modification</param>
/// <param name="anchor">Anchor</param>
/// <returns>New url</returns>
string ModifyQueryString(string url, string queryStringModification, string anchor); /// <summary>
/// Url中移除指定的查询字符串
/// </summary>
/// <param name="url">Url to modify</param>
/// <param name="queryString">Query string to remove</param>
/// <returns>New url</returns>
string RemoveQueryString(string url, string queryString); /// <summary>
/// Url获取参数名称对应的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name">Parameter name</param>
/// <returns>Query string value</returns>
T QueryString<T>(string name); /// <summary>
/// 重启应用程序
/// 该方法在商城重启中会用到
/// </summary>
/// <param name="makeRedirect">A value indicating whether we should made redirection after restart</param>
/// <param name="redirectUrl">Redirect URL; empty string if you want to redirect to the current page URL</param>
void RestartAppDomain(bool makeRedirect = false, string redirectUrl = ""); /// <summary>
/// Gets a value that indicates whether the client is being redirected to a new location
/// 获取指示客户端是否重定向到新位置的值。
/// 如果位置响应标头的值与当前位置不同,则为 true;否则为 false。
/// </summary>
bool IsRequestBeingRedirected { get; } /// <summary>
/// Gets or sets a value that indicates whether the client is being redirected to a new location using POST
/// Post请求时获取指示客户端是否重定向到新位置的值。
/// </summary>
bool IsPostBeingDone { get; set; }
}
}

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

 using System.Collections.Specialized;
using System.Web;
using Nop.Core.Fakes;
using Nop.Tests;
using NUnit.Framework; namespace Nop.Core.Tests
{
[TestFixture]
public class WebHelperTests
{
private HttpContextBase _httpContext;
private IWebHelper _webHelper; [Test]
public void Can_get_serverVariables()
{
var serverVariables = new NameValueCollection();
serverVariables.Add("Key1", "Value1");
serverVariables.Add("Key2", "Value2");
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
_webHelper = new WebHelper(_httpContext);
_webHelper.ServerVariables("Key1").ShouldEqual("Value1");
_webHelper.ServerVariables("Key2").ShouldEqual("Value2");
_webHelper.ServerVariables("Key3").ShouldEqual("");
} [Test]
public void Can_get_storeHost_without_ssl()
{
var serverVariables = new NameValueCollection();
serverVariables.Add("HTTP_HOST", "www.example.com");
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
_webHelper = new WebHelper(_httpContext);
_webHelper.GetStoreHost(false).ShouldEqual("http://www.example.com/");
} [Test]
public void Can_get_storeHost_with_ssl()
{
var serverVariables = new NameValueCollection();
serverVariables.Add("HTTP_HOST", "www.example.com");
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
_webHelper = new WebHelper(_httpContext);
_webHelper.GetStoreHost(true).ShouldEqual("https://www.example.com/");
} [Test]
public void Can_get_storeLocation_without_ssl()
{
var serverVariables = new NameValueCollection();
serverVariables.Add("HTTP_HOST", "www.example.com");
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
_webHelper = new WebHelper(_httpContext);
_webHelper.GetStoreLocation(false).ShouldEqual("http://www.example.com/");
} [Test]
public void Can_get_storeLocation_with_ssl()
{
var serverVariables = new NameValueCollection();
serverVariables.Add("HTTP_HOST", "www.example.com");
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
_webHelper = new WebHelper(_httpContext);
_webHelper.GetStoreLocation(true).ShouldEqual("https://www.example.com/");
} [Test]
public void Can_get_storeLocation_in_virtual_directory()
{
var serverVariables = new NameValueCollection();
serverVariables.Add("HTTP_HOST", "www.example.com");
_httpContext = new FakeHttpContext("~/nopCommercepath", "GET", null, null, null, null, null, serverVariables);
_webHelper = new WebHelper(_httpContext);
_webHelper.GetStoreLocation(false).ShouldEqual("http://www.example.com/nopcommercepath/");
} [Test]
public void Get_storeLocation_should_return_lowerCased_result()
{
var serverVariables = new NameValueCollection();
serverVariables.Add("HTTP_HOST", "www.Example.com");
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, serverVariables);
_webHelper = new WebHelper(_httpContext);
_webHelper.GetStoreLocation(false).ShouldEqual("http://www.example.com/");
} [Test]
public void Can_get_queryString()
{
var queryStringParams = new NameValueCollection();
queryStringParams.Add("Key1", "Value1");
queryStringParams.Add("Key2", "Value2");
_httpContext = new FakeHttpContext("~/", "GET", null, null, queryStringParams, null, null, null);
_webHelper = new WebHelper(_httpContext);
_webHelper.QueryString<string>("Key1").ShouldEqual("Value1");
_webHelper.QueryString<string>("Key2").ShouldEqual("Value2");
_webHelper.QueryString<string>("Key3").ShouldEqual(null);
} [Test]
public void Can_remove_queryString()
{
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
_webHelper = new WebHelper(_httpContext);
//first param (?)
_webHelper.RemoveQueryString("http://www.example.com/?param1=value1&param2=value2", "param1")
.ShouldEqual("http://www.example.com/?param2=value2");
//second param (&)
_webHelper.RemoveQueryString("http://www.example.com/?param1=value1&param2=value2", "param2")
.ShouldEqual("http://www.example.com/?param1=value1");
//non-existing param
_webHelper.RemoveQueryString("http://www.example.com/?param1=value1&param2=value2", "param3")
.ShouldEqual("http://www.example.com/?param1=value1&param2=value2");
} [Test]
public void Can_remove_queryString_should_return_lowerCased_result()
{
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
_webHelper = new WebHelper(_httpContext);
_webHelper.RemoveQueryString("htTp://www.eXAmple.com/?param1=value1&parAm2=value2", "paRAm1")
.ShouldEqual("http://www.example.com/?param2=value2");
} [Test]
public void Can_remove_queryString_should_ignore_input_parameter_case()
{
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
_webHelper = new WebHelper(_httpContext);
_webHelper.RemoveQueryString("http://www.example.com/?param1=value1&parAm2=value2", "paRAm1")
.ShouldEqual("http://www.example.com/?param2=value2");
} [Test]
public void Can_modify_queryString()
{
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
_webHelper = new WebHelper(_httpContext);
//first param (?)
_webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param1=value3", null)
.ShouldEqual("http://www.example.com/?param1=value3&param2=value2");
//second param (&)
_webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param2=value3", null)
.ShouldEqual("http://www.example.com/?param1=value1&param2=value3");
//non-existing param
_webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param3=value3", null)
.ShouldEqual("http://www.example.com/?param1=value1&param2=value2&param3=value3");
} [Test]
public void Can_modify_queryString_with_anchor()
{
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
_webHelper = new WebHelper(_httpContext);
_webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2", "param1=value3", "Test")
.ShouldEqual("http://www.example.com/?param1=value3&param2=value2#test");
} [Test]
public void Can_modify_queryString_new_anchor_should_remove_previous_one()
{
_httpContext = new FakeHttpContext("~/", "GET", null, null, null, null, null, null);
_webHelper = new WebHelper(_httpContext);
_webHelper.ModifyQueryString("http://www.example.com/?param1=value1&param2=value2#test1", "param1=value3", "Test2")
.ShouldEqual("http://www.example.com/?param1=value3&param2=value2#test2");
}
}
}

Nop.Core.Tests.WebHelperTests

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

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

上一篇:java中Class.forName("xxx")和ClassLoader().loadClass("xxx")的区别


下一篇:【笔记】【网络流】原始对偶算法中调整的必要性与正确性的证明