Web 表单中的 URL 映射 - 允许使用问号和连字符

发布于 2025-01-05 07:52:07 字数 1266 浏览 0 评论 0原文

我已在 ASP.NET 4 应用程序中实现了 URL 映射,但某些内容存在问题。

我们的一些产品带有连字符“-”或问号“?”在他们之中。这不是删除它的选项。产品名称可以是“我的产品 - 对您有好处吗?”。

我们使用两个自定义方法,MakeUrlSeoReadyMakeUrlNonSeoReady。我们像这样替换空格:Replace(" ","-"),因为这是对 SEO 最友好的解决方案。然而,我们还需要使用问号和连字符来完成这项工作。

我们使用 MakeUrlSeoReady / NonReady 方法的原因是为了能够显示“真实”名称。

目前映射定义如下:

routes.MapPageRoute("Produkt visning",
                    "artikler/{Categoryname}/{SubCategoryname}/{ProductName}",
                    "~/SingleProduct.aspx");

所以我所做的就是根据 ProductName 检索产品。我使用我创建的两种方法:

    public static string MakeUrlNonSeoReady(string text)
    {
        return text.ToLower().
           Replace("oe", "ø").
           Replace("aa", "å").
           Replace("ae", "æ").
           Replace("-", " ");
    }

    public static string MakeUrlSeoReady(string text)
    {
        return text.ToLower().
            Replace("ø", "oe").
            Replace("å", "aa").
            Replace("æ", "ae").
            Replace(" ", "-");
    }

因此,在 SingleProduct.aspx 页面中,我使用以下字符串从数据库中获取:

string categoryName = HelperFunctions.MakeUrlNonSeoReady(Page.RouteData.Values["ProductName"]);

但这当然行不通。所以非常感谢任何帮助:-)

I have implemented URL mapping in our ASP.NET 4 application, but I have a problem with some of our content.

Some of our products has a hyphen "-" or a question mark "?" in them. It's not an option to remove that. A productname could be "My Product - Good for you?".

We use two custom made methods, MakeUrlSeoReady and MakeUrlNonSeoReady. We replace space like this: Replace(" ","-"), as this is the most SEO-friendly solution. However, we also need to make this work with both question marks and hyphens.

The reason we use the MakeUrlSeoReady / NonReady methods is to be able to show the "real" name.

Currently the mapping is defined as follows:

routes.MapPageRoute("Produkt visning",
                    "artikler/{Categoryname}/{SubCategoryname}/{ProductName}",
                    "~/SingleProduct.aspx");

So what I do is I retrieve the product depending on the ProductName. I use two methods I've created:

    public static string MakeUrlNonSeoReady(string text)
    {
        return text.ToLower().
           Replace("oe", "ø").
           Replace("aa", "å").
           Replace("ae", "æ").
           Replace("-", " ");
    }

    public static string MakeUrlSeoReady(string text)
    {
        return text.ToLower().
            Replace("ø", "oe").
            Replace("å", "aa").
            Replace("æ", "ae").
            Replace(" ", "-");
    }

So in the SingleProduct.aspx page I use the following string to get from our database:

string categoryName = HelperFunctions.MakeUrlNonSeoReady(Page.RouteData.Values["ProductName"]);

But this will of course not work. So any help is really appreciated :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如日中天 2025-01-12 07:52:07

一种可以说更干净、更简单的方法是使用数字或字母数字且原生 HTML 编码的唯一产品标识符,然后简单地将产品名称作为未使用的参数用于 SEO 或搜索目的。

MSDN RouteCollection.MapPageRoute 方法(字符串、字符串、字符串、布尔值、RouteValueDictionary)

routes.MapPageRoute("Produkt visning", 
                "artikler/{Categoryname}/{SubCategoryname}/{ProductIdentifier}/{ProductName}", 
                "~/SingleProduct.aspx", false, new RouteValueDictionary 
    { { "ProductName ", string.Empty } }); 

An arguably cleaner and simpler method is to use a unique product identifier that is numerical or alphanumerical and is natively HTML encoded, and then simply put the product name as an unused parameter for SEO or search purposes.

MSDN RouteCollection.MapPageRoute Method (String, String, String, Boolean, RouteValueDictionary)

routes.MapPageRoute("Produkt visning", 
                "artikler/{Categoryname}/{SubCategoryname}/{ProductIdentifier}/{ProductName}", 
                "~/SingleProduct.aspx", false, new RouteValueDictionary 
    { { "ProductName ", string.Empty } }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文