using System.Web;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace HuiNong.Code
{
/// <summary>
///
/// </summary>
public class Help
{/// <summary>
/// 对url执行修改参数的任务参数格式为 (name_value)(name2_value2)
/// </summary>
/// <param name="perfix">要修改或增加的参数的名称</param>
/// <param name="value">要修改或增加的参数</param>
/// <param name="url">要修改的url</param>
/// /// <param name="removeitem">解决冲突</param>
/// <returns></returns>
static public string urlcorrect(Dictionary<string,string> collection, string url,bool removeQuestionMark=false)
{
var questionword = "";
var indexx = url.IndexOf("?");
if (indexx != -1) {
questionword = url.Substring(indexx);
url= url.Substring(0, indexx);
}
foreach (var item in collection) {
//当找到参数时
Regex regex = new Regex("\\(" + item.Key + "_.+?\\)");
if (regex.Match(url).Success) { url = regex.Replace(url, "("+item.Key + "_" + item.Value + ")"); continue; }
//当没有找到参数时,添加参数
string inputword = "/("+item.Key + "_" + item.Value+")";
url += inputword;
Regex regex2 = new Regex("\\)/\\(");
if (regex2.Match(url).Success) { url = regex2.Replace(url, ")("); continue; }
}
if (!removeQuestionMark) {
url += questionword;
}
return url;
}
static public string UrlgetValue(string words,string name) {
//string[] word = words.Split('+');
//string returnvalue = null;
//foreach (var item in word) {
// var name_value=item.Split('_');
// if (name_value[0].Trim() == name.Trim()) return name_value[1];
//}
string returnvalue = null;
Regex reg = new Regex("(?<=\\()"+name+"_\\w{1,}(?=\\))");
Match xx= reg.Match(words);
if (xx.Length!=0) {
returnvalue= xx.Value.Split('_')[1];
returnvalue = returnvalue.Split('^')[0];
}
return returnvalue;
}
/// <summary>
/// 选中按钮
/// </summary>
/// <param name="name">area</param>
/// <param name="value">区域值</param>
/// <param name="request">传入request</param>
/// <returns></returns>
static private bool activeA(string name,string value,HttpRequestBase request) {
Regex regex = new Regex("\\("+name+"_"+value+"\\)");
//string matchword = name + "_" + value;
if (regex.Match(request.RawUrl).Success) {
return true;
}
return false;
}/// <summary>
/// 可能会出现url中没有该参数的时候应该把按钮置为全选状态
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="request"></param>
/// <param name="defaultchosen">当为true则当url中没有该参数时该按钮置为选中</param>
/// <returns></returns>
static public bool activeA(string name, string value, HttpRequestBase request,bool defaultchosen = false)
{
if (defaultchosen) {
Regex regex = new Regex("\\(" + name + "_\\w*\\)");
if (regex.Match(request.RawUrl).Success)
{
return false;
}
else {
return true;
}
}
else
{
return activeA(name, value, request);
}
}
static public string urlcorrect(Dictionary<string, string> collection, HttpRequestBase request, bool removeQuestionMark = false)
{
//var url = request.Url.AbsoluteUri;
var path = request.Path;
// url = url.Substring(url.IndexOf("/"));
var url = request.RawUrl;
url = url.Substring(url.IndexOf(path));
var correctedURL= urlcorrect(collection, url, removeQuestionMark);
return correctcata(correctedURL);
}
//修正在storedetail页中出现的错误
public static string correctcata(string url)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("/storedetail/\\d+/");
if (regex.Match(url).Success) { url = regex.Replace(url, "/stores/"); };
return url;
}
}
}
使用方法
在mvc里
添加
Dictionary<string, string> collection = new Dictionary<string, string>();
collection.Add("area", item.id.ToString());
<a class="@(Help.activeA("area",item.id.ToString(),Request)?"active":"")" href="@Help.urlcorrect(collection, Request, true)">@item.name</a>
//或者当url中没有该参数该参数时选中本a标签的
<a class="@(Help.activeA("area",item.id.ToString(),Request,true)?"active":"")" href="@Help.urlcorrect(collection, Request, true)">@item.name</a>
@(Help.activeA(“area”,item.id.ToString(),Request)?”active”:”“)的作用是:当url里面有(area_id的值)时,把class设为active
@Help.urlcorrect(collection, Request, true) 的作用是 把area和id值放到url里,如果area已经存在则修改它的值
表现形式为
http://xxxxx/home/xxx/(type_19)(industry_-1)(searchword_-1)(ishuinong_0)