接口: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

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

nopCommerce 3.9 大波浪系列 之 IWebHelper的更多相关文章

  1. nopCommerce 3.9 大波浪系列 之 引擎 NopEngine

    本章涉及到的内容如下 1.EngineContext初始化IEngine实例 2.Autofac依赖注入初始化 3.AutoMapper框架初始化 4.启动任务初始化 一.EngineContext初 ...

  2. nopCommerce 3.9 大波浪系列 之 路由扩展 [多语言Seo的实现]

    一.nop种的路由注册 在Global.asax,Application_Start()方法中会进行路由注册,代码如下. public static void RegisterRoutes(Route ...

  3. nopCommerce 3.9 大波浪系列 之 网页加载Widgets插件原理

    一.插件简介 插件用于扩展nopCommerce的功能.nopCommerce有几种类型的插件如:支付.税率.配送方式.小部件等(接口如下图),更多插件可以访问nopCommerce官网. 我们看下后 ...

  4. nopCommerce 3.9 大波浪系列 之 开发支持多店的插件

    一.基础介绍 nop支持多店及多语言,本篇结合NivoSlider插件介绍下如何开发支持多商城的小部件. 主要接口如下: ISettingService 接口:设置接口,可实现多店配置. (点击接口介 ...

  5. nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存

    一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...

  6. nopCommerce 3.9 大波浪系列 之 使用部署在Docker中的Redis缓存主从服务

    一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...

  7. nopCommerce 3.9 大波浪系列 之 global.asax

    一.nop的global.asax文件 nop3.9基于ASP.NET MVC 5框架开发,而ASP.NET MVC中global.asax文件包含全局应用程序事件的事件处理程序,它响应应用程序级别和 ...

  8. nopCommerce 3.9 大波浪系列 之 汉化-Roxy Fileman

    官网:http://www.roxyfileman.com/ 中文包:zh.json 1.将zh.json包拷贝到Nop.Admin项目中"Content\Roxy_Fileman\lang ...

  9. nopCommerce 3.9 大波浪系列 之 事件机制(生产者、消费者)

    一.nop事件机制简介 应用场景:客户支付成功后,需要发送短信.邮件告知客户订单支付成功(短信.邮件由不同模块实现) 实现方法: 1.定义支付成功OrderPaidEvent事件. 2.定义短信,邮箱 ...

随机推荐

  1. 2-SAT算法

    参考blog 参考论文 参考论文 题目 & 题解 裸2-SAT poj3683 poj3207 poj3678 poj3648 2-SAT + 二分法 poj2723 poj2749 hdu3 ...

  2. Java 9 揭秘(2. 模块化系统)

    文 by / 林本托 Tips 做一个终身学习的人. 在此章节中,主要介绍以下内容: 在JDK 9之前Java源代码用于编写,打包和部署的方式以及该方法的潜在问题 JDK 9中有哪些模块 如何声明模块 ...

  3. 一天搞定CSS:定位position--17

    1.定位取值概览 2.相对定位relative <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  4. 隐马尔科夫模型HMM(一)HMM模型

    隐马尔科夫模型HMM(一)HMM模型基础 隐马尔科夫模型HMM(二)前向后向算法评估观察序列概率 隐马尔科夫模型HMM(三)鲍姆-韦尔奇算法求解HMM参数(TODO) 隐马尔科夫模型HMM(四)维特比 ...

  5. 事件总线(Event Bus)知多少

    源码路径:Github-EventBus 简书同步链接 1. 引言 事件总线这个概念对你来说可能很陌生,但提到观察者(发布-订阅)模式,你也许就很熟悉.事件总线是对发布-订阅模式的一种实现.它是一种集 ...

  6. java 1.8 动态代理源码分析

    JDK8动态代理源码分析 动态代理的基本使用就不详细介绍了: 例子: class proxyed implements pro{ @Override public void text() { Syst ...

  7. htm语言的语法基础及规则

    HTML的主要语法是元素和标签.元素是符合DTD(文档类型定义)的文档组成部分,如title(文档标题).IMG(图象).table(表格)等等.元素名不区分大小写的.HTML用标签来规定元素的属性和 ...

  8. 使用SQL Server 发送邮件

    在很多数据分析和集成的场景下,我们需要了解数据库中关键的脚本或者job的执行情况.这个时候邮件提醒是一种比较不错的通知方式.本文从零开始,一步一步的介绍如何使用SQL Server来发送邮件. 环境: ...

  9. VR全景智慧城市常诚:信息技术点亮“智慧城市”

    亚太城市峰会暨市长论坛日前在澳大利亚昆士兰州首府布里斯班举行,"智慧城市"成为焦点议题.来自135个国家和地区的市长.副市长及代表们共同讨论如何利用高新科技解决城市发展中的问题,让 ...

  10. 利用canvas压缩图片

    现在手机拍的照片动不动就是几M,当用户上传手机里的照片时一个消耗流量大,一个上传时间长,为了解决这个问题,就需要压缩图片: 想法:利用canvas重绘图片,保持宽高比不变,具体宽高根本具体情况而定. ...