【问题标题】:Can't reference Regex in string extension method无法在字符串扩展方法中引用正则表达式
【发布时间】:2011-06-11 01:21:26
【问题描述】:

我遇到了一个很奇怪的问题: 我为这样的字符串做了扩展方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vaniv.Mvc
{
    public static class StringHelpers
    {
        public static string ToSeoUrl(this string url)
        {
            // make the url lowercase
            string encodedUrl = (url ?? "").ToLower();
            System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9 -]");
            encodedUrl = rgx.Replace(encodedUrl, "-");

            return encodedUrl;
        }
    }
}

在运行时出现错误的问题: CS0246:找不到类型或命名空间名称“Regex”(您是否缺少 using 指令或程序集引用?)

我没有错过 using 指令。我也没有错过大会(例如,我可以使用 Regex withing 控制器)。 我已将我的扩展方法放入 App_Code,但怀疑它是否有任何联系,

【问题讨论】:

  • 您是否尝试过清理您的解决方案并强制重建?
  • 并不是说这完全解决了你的问题,但是如果你在网址上使用.ToLower(),为什么你的正则表达式中有A-Z
  • 尝试将using移动到namespace内部

标签: c# regex


【解决方案1】:

将cs文件移动到另一个目录(App_Code文件夹外),放在项目的根目录下。

查看this article

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2019-11-05
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多