简体   繁体   English

如何在 asp.net Url 路由中用连字符“-”替换空格,同时从 html (.aspx)页面传递参数

[英]how to replace space with hyphen '-' in asp.net Url routing while passing parameter from html (.aspx) page

I have the following pages:我有以下页面:

  1. landing_page.aspx (products page) login_page.aspx(产品页面)
  2. product_description.aspx (for showing the product description) product_description.aspx(用于显示产品描述)

I am passing my product name from the landing_page to the product_description page on the url like this:我将我的产品名称从landing_page 传递到 url 上的 product_description 页面,如下所示:

<a href="product-description/<%#Eval("Product_name")%>" >

where "product-description" is my URL routing string.其中“产品描述”是我的 URL 路由字符串。

I am getting these results:我得到这些结果:

http://localhost:33891/product-description/shifon %20scarf%201%20meter

or或者

http://localhost:33891/product-description/shifon%20scarf%201%20meter

I want it to be like this:我希望它是这样的:

http://localhost:33891/product-description/shifon-scarf-1-meter http://localhost:33891/product-description/shifon-scarf-1-meter

The problem is that the names of my products have space in between which becomes %20 in the url.问题是我的产品名称之间有空格,在 url 中变为 %20。 How should I replace the space or %20 with Hyphens '-' before passing it to URL?在将空格或 %20 传递给 URL 之前,我应该如何用连字符“-”替换空格或 %20?

you can pass the return of this function你可以通过这个 function 的返回

public static string ClearnUrl(string title)
    {
        string Product_name = title.ToLower().Replace(" ", "-");
        //Removes invalid character like .,-_ etc
        Product_name = Regex.Replace(Product_name, @"[^a-zA-Z0-9\/_|+ -]", ""); 
        return Product_name;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM