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¶m2=value2", "param1")
.ShouldEqual("http://www.example.com/?param2=value2");
//second param (&)
_webHelper.RemoveQueryString("http://www.example.com/?param1=value1¶m2=value2", "param2")
.ShouldEqual("http://www.example.com/?param1=value1");
//non-existing param
_webHelper.RemoveQueryString("http://www.example.com/?param1=value1¶m2=value2", "param3")
.ShouldEqual("http://www.example.com/?param1=value1¶m2=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¶m2=value2", "param1=value3", null)
.ShouldEqual("http://www.example.com/?param1=value3¶m2=value2");
//second param (&)
_webHelper.ModifyQueryString("http://www.example.com/?param1=value1¶m2=value2", "param2=value3", null)
.ShouldEqual("http://www.example.com/?param1=value1¶m2=value3");
//non-existing param
_webHelper.ModifyQueryString("http://www.example.com/?param1=value1¶m2=value2", "param3=value3", null)
.ShouldEqual("http://www.example.com/?param1=value1¶m2=value2¶m3=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¶m2=value2", "param1=value3", "Test")
.ShouldEqual("http://www.example.com/?param1=value3¶m2=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¶m2=value2#test1", "param1=value3", "Test2")
.ShouldEqual("http://www.example.com/?param1=value3¶m2=value2#test2");
}
}
}
Nop.Core.Tests.WebHelperTests
本文地址:http://www.cnblogs.com/yaoshangjin/p/nopCommerce39.html
本文为大波浪原创、转载请注明出处。
nopCommerce 3.9 大波浪系列 之 IWebHelper的更多相关文章
- nopCommerce 3.9 大波浪系列 之 引擎 NopEngine
本章涉及到的内容如下 1.EngineContext初始化IEngine实例 2.Autofac依赖注入初始化 3.AutoMapper框架初始化 4.启动任务初始化 一.EngineContext初 ...
- nopCommerce 3.9 大波浪系列 之 路由扩展 [多语言Seo的实现]
一.nop种的路由注册 在Global.asax,Application_Start()方法中会进行路由注册,代码如下. public static void RegisterRoutes(Route ...
- nopCommerce 3.9 大波浪系列 之 网页加载Widgets插件原理
一.插件简介 插件用于扩展nopCommerce的功能.nopCommerce有几种类型的插件如:支付.税率.配送方式.小部件等(接口如下图),更多插件可以访问nopCommerce官网. 我们看下后 ...
- nopCommerce 3.9 大波浪系列 之 开发支持多店的插件
一.基础介绍 nop支持多店及多语言,本篇结合NivoSlider插件介绍下如何开发支持多商城的小部件. 主要接口如下: ISettingService 接口:设置接口,可实现多店配置. (点击接口介 ...
- nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存
一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...
- nopCommerce 3.9 大波浪系列 之 使用部署在Docker中的Redis缓存主从服务
一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...
- nopCommerce 3.9 大波浪系列 之 global.asax
一.nop的global.asax文件 nop3.9基于ASP.NET MVC 5框架开发,而ASP.NET MVC中global.asax文件包含全局应用程序事件的事件处理程序,它响应应用程序级别和 ...
- nopCommerce 3.9 大波浪系列 之 汉化-Roxy Fileman
官网:http://www.roxyfileman.com/ 中文包:zh.json 1.将zh.json包拷贝到Nop.Admin项目中"Content\Roxy_Fileman\lang ...
- nopCommerce 3.9 大波浪系列 之 事件机制(生产者、消费者)
一.nop事件机制简介 应用场景:客户支付成功后,需要发送短信.邮件告知客户订单支付成功(短信.邮件由不同模块实现) 实现方法: 1.定义支付成功OrderPaidEvent事件. 2.定义短信,邮箱 ...
随机推荐
- 一天搞定CSS: 浮动(float)及文档流--10
浮动(float),一个我们即爱又恨的属性.爱,因为通过浮动,我们能很方便地布局: 恨,浮动之后遗留下来太多的问题需要解决,特别是IE6-7(以下无特殊说明均指 windows 平台的 IE浏览器). ...
- nodejs实战:使用原生nodeJs模块实现静态文件及REST请求解析及响应(基于nodejs6.2.0版本,不使用express等webMVC框架 )
一.准备工作 1.安装nodejs 首先你需要安装nodeJs 那么nodejs官网:http://nodejs.cn/,下载相应版本,一步一步安装. 二.使用nodejs开发服务器后台应用 1.创建 ...
- django 自定义过滤器(filter)处理较为复杂的变量的实例
简述:django 在views中有数据需要通过字典(dict)的方式传递给template,该字典中又包含了字典,而且字典中的键值还是一个对象,在template中处理传递过来的数据的时候,字典不能 ...
- Macbook怎么强制关闭后台程序?Macbook强制关闭后台程序的方法
有时候我们的Macbook电脑运行某个程序卡在那里耗了很长时间,程序本身有可能提供了取消按钮,点了也没有反应,这时候我们就很想强制关闭它了,那么Macbook怎么强制关闭后台运行的程序呢?下面完美小编 ...
- JS问题笔记——模拟Jq底层实现工厂模式
<script type="text/javascript"> (function (window,undefined){ function _$(arguments) ...
- thinkphp5.0学习笔记(二)
本文为公司制作API接口后台的小结! 1.命名注意事项: 不要使用易混淆的名字,如index,index01... 我喜欢用拼音... 比如: public function zhuce(Reques ...
- Bash中单引号和双引号的区别
单引号和双引号的区别 单引号:必须成对使用,它可以保护所有的字符不被翻译.如变量$1,和奇数个单引号的作用相同,偶数个单引号=1个双引号双引号:必须成对出现,它可以保护一些元字符不被翻译,但允许变量和 ...
- 无法启动此程序因为计算机中丢失msvcr71
http://jingyan.baidu.com/article/25648fc1abc4d69190fd0077.html 下面是msvcr文件下载地址: 链接:http://pan.b ...
- jenkins管理员密码登录不了
1.密码管理员密码,如何修改 进入/var/jenkins_home/users/admin目录下修改config.xml文件: 以下密码是admin <hudson.security.Huds ...
- 通过位异或来交换a,b的值和通过中间变量交换a,b的值
//通过位异或来交换a,b的值 #include <stdio.h> int main(int argc, const char * argv[]) { int a=20,b=10; ...