接口: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. vue-cli webpack在node环境下安装使用

    第一步,需要下载并安装node.js以及他的npm组件: 第二步,用node -v;npm -v来测试node.js以及npm是否安装成功(建议用GIT命令行工具,因为GIT是linux系统),如果显 ...

  2. Xamarin开发笔记—百度在线语音合成

    语音合成:也被称为文本转换技术(TTS),它是将计算机自己产生的.或外部输入的文字信息转变为可以听得懂的.流利的口语输出的技术. 技术选型:语音合成初步选择有两个,一是讯飞.二是百度. 因为使用的是X ...

  3. final关键字(最终的)

    1.fianl关键字可以用来修饰类.属性.方法: 2.final关键字修饰的类不能再继承: 如:String   String类是final修饰的类不能被继承 3.final关键字修饰的方法不能被重写 ...

  4. Java之IO流详解

    IO流 Input/Output 完成输入/输出 应用程序运行时——数据在内存中  ←→ 把数据写入硬盘(磁带)  内存中的数据不可持久保存的  输入:从外部存储器(硬盘.磁带.U盘)把数据读入内存. ...

  5. Thrift中required和optional

    最近在搞Thrift,对其字段声明中的required关键字有所误解,仔细调试了一下才明白其真实含义. required的意思不是说声明对象时,必须填这个值,而是Thrift在传输(序列化)过程中无论 ...

  6. .Net中的AOP系列之《将AOP作为架构工具》

    返回<.Net中的AOP>系列学习总目录 本篇目录 编译时初始化和验证 编译时初始化 切面验证的正确用法 真实案例:复习线程 架构约束 强制架构 真实案例:NHibernate 多播 类级 ...

  7. Cordova各个插件使用介绍系列(四)—canvas2ImagePlugin保存二维码到手机本地

    详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-4-canvas2imageplugin/ 在前面几篇 ...

  8. ionic 项目中添加modal的步骤流程

    1.首先在templates文件夹下面定义一个新页面,xxx.html,template文件夹在空项目里面是没有的,需要手动添加一个,放在WWW文件夹下面. <ion-modal-view> ...

  9. Mysql数据库存储emoji表情

    emoji表情需要使用编码格式未utf8mb4,mysql数据库版本要5.5以上,我用的是5.6,因为只有5.5以上支持utf8mb4. 1.数据库编码设定为utf8mb4,如果建库时指定的是utf8 ...

  10. ADO.NET中的五大对象

    Connection connection 对象主要是开启程序和数据库之间的连接.没有利用连接对象将数据库打开,是无法从数据库中取到数据的.这个物件是ADO.NET的最底层,我们可以自己产生这个对象, ...