接口: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. hive报错 Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:For direct MetaStore DB connections,

    学习hive 使用mysql作为元数据  hive创建数据库和切换数据库都是可以的 但是创建表就是出问题 百度之后发现 是编码问题 特别记录一下~~~ 1.报错前如图: 2.在mysql数据库中执行如 ...

  2. UX2 beta 3正式发布!!

    UX浏览服务是为了加速浏览网页而开发的浏览服务,它解决了WebView的一系列问题,它能够在网络差的情况下快速的浏览,比webview快一倍以上,是webview的优化代替方案.它拥有完善的缓存管理策 ...

  3. js实现整数转化为小数

    toFixed 方法 返回一个字符串,代表一个以定点表示法表示的数字. number .toFixed(i) 参数 bumber 必选项.一个 Number 对象. i 可选项.小数点 后的数字位数. ...

  4. css样式表的选择器与分类

    css 样式表的作用: 主要用于结构,样式与行为,CSS主要的作用就是美化网页的一个语言,它的特点: 1.结构与样式分离的方式,便于后期维护与改版; 2.样式定义精确到像素的级别; css样式表的结构 ...

  5. JS常用数据校验集合(adding)

    常用数据校验集合 var _validator = { MAIL_REGEX: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,; ...

  6. 基于FPGA的IIR滤波器

    基于FPGA的IIR滤波器                                                         by方阳 版权声明:本文为博主原创文章,转载请指明转载地址 ...

  7. salesforce零基础学习(七十三)ProcessInstanceWorkItem/ProcessInstanceStep/ProcessInstanceHistory浅谈

    对于审批流中,通过apex代码进行审批操作一般都需要获取当前记录对应的ProcessInstanceWorkitem或者ProcessInstanceStep然后执行Approval.process操 ...

  8. Java 基础 程序流程控制 (下)

    Java 程序流程控制 (下) 此篇单独对循环结构的知识点进行整理: 之前讲到循环结构分为:for循环,while循环,do...while循环三种最基本的循环结构:在JDK1.5以后的版本还提供了f ...

  9. Entity Framework入门教程: Entity Framework支持的查询方式

    Entity Framework支持的查询方式有三种 LINQ to Entities Entity SQL Native SQL [LINQ to Entities] LINQ(语言集成查询)是从V ...

  10. 利用python将mysql中的数据导入excel

    Python对Excel的读写主要有xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种. 如下分别利用xlwt和openpyxl将mysql数据库中查询的数据保存到exce ...