简体   繁体   English

ASP.NET MVC / C#-有效URL字符的字符串?

[英]ASP.NET MVC / C# - String to valid URL characters?

I don't know how to ask this, and I don't know what it is called either so I'll just describe what I want to achieve. 我不知道该怎么问,我也不知道这叫什么,所以我只描述我想要实现的目标。

In the database, some articles' title originaly has spaces: 在数据库中,某些文章的标题原始具有空格:

my title with spaces

But in the url, spaces are replaced by other characters such as plus sign (+) or underscore (_) 但是在url中,空格被其他字符替换,例如加号(+)或下划线(_)

http://www.mydomain.com/mycontroller/myaction/my_title_with_spaces

or 要么

http://www.mydomain.com/mycontroller/myaction/my+title+with+spaces

Now, how do you do that in C#? 现在,您如何在C#中做到这一点? Or is there any helper in ASP.NET MVC that can do something like that? 还是ASP.NET MVC中有任何可以执行类似操作的帮助程序?

Let say we achieved the said URL, is there any risk that two unique titles become the same in the URL? 假设我们达到了上述网址,那么网址中是否有两个唯一标题相同的风险? Please consider these titles: 请考虑以下标题:

Title's
Titles

after parsing, they became the same 解析后,它们变得一样

Titles Titles 标题标题

This will be a problem when retrieving the article from the database since I'll get two results, one for "Title" and one for "Title's". 当从数据库中检索文章时,这将是一个问题,因为我将获得两个结果,一个结果为“标题”,一个结果为“标题”。

I would implement that functionality like this: 我将实现以下功能:

1. When creating a new article, generate the URL representation based on the title. 1.在创建新文章时,根据标题生成URL表示。

Use a function that converts the title for a suitable representation. 使用将标题转换为合适表示形式的函数。 For example, the title "This is an example" might generate something like "This_is_an_example". 例如,标题“这是一个示例”可能会生成类似“ This_is_an_example”的内容。

This is up to you. 这取决于你。 You can create a function that parses the title with rules you define, or use an existing one if it suits better your problem. 您可以创建一个函数,该函数使用定义的规则来解析标题,或者使用一个更适合您的问题的函数。

2. Ensure the URL representation is unique 2.确保URL表示唯一

If it's going to be an ID, it must be unique. 如果它将是一个ID,则它必须是唯一的。 So, when creating new articles you must query your database for the resulting URL representation. 因此,在创建新文章时,必须在数据库中查询生成的URL表示形式。 If you get a result from the database, it means the newly created article generated the same representation as one of the already created articles. 如果从数据库中获得结果,则意味着新创建的文章与已创建的文章之一生成了相同的表示形式。 Add something to it so it remains unique. 添加一些东西,使其保持唯一。

This could be something like "This_is_an_example_2". 可能类似于“ This_is_an_example_2”。 In this case, we added the "_2" to the end of the generated representation so it differs from the already existing one. 在这种情况下,我们在生成的表示的末尾添加了“ _2”,因此它与现有的表示不同。 Once more, with each change you have to ensure this representation remains unique. 再一次,您必须确保每次更改都保持唯一性。

3. Save the created ID in the database, along with the article data 3.将创建的ID以及商品数据保存在数据库中

In the database be sure to save the "This_is_an_example" ID and relate it to the article. 在数据库中,请确保保存“ This_is_an_example” ID,并将其与文章相关联。 Maybe even as the table primary key? 甚至可以作为表的主键?

4. Query the database for the correct article 4.在数据库中查询正确的文章

Now, about showing a site visitor the correct article: When a visitor asks for the following resource, for example: 现在,关于向网站访问者显示正确的文章:例如,当访问者要求以下资源时:

http://www.mydomain.com/mycontroller/myaction/this_is_an_example_2

Extract the URL part that identifies the article, in this case "this_is_an_example_2". 提取标识文章的URL部分,在本例中为“ this_is_an_example_2”。

When you have that, you have the identifier of the article in the database. 有了该标签后,就可以在数据库中获得该文章的标识符。 So, you can query the database for the article with the "this_is_an_example_2" ID and show the article's content to the user. 因此,您可以在数据库中查询具有“ this_is_an_example_2” ID的文章,并向用户显示文章的内容。

This might involve some URL rewriting. 这可能涉及一些URL重写。 Unfortunately I'm unable to help you with that in asp.NET. 不幸的是,我无法在asp.NET中为您提供帮助。 Some search on the subject will surely help you. 在该主题上进行一些搜索一定会对您有所帮助。

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

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