逻辑想法 - 使用 C# 和 WCF 构建 URL 缩短服务
我需要一些想法如何使用 WCF 以及 sql server 作为数据库来实现 URL 缩短服务。
我将使用 WCF 处理我的 url 缩短逻辑。该项目将处理为 url 创建短别名,并将它们与实际 url 一起写入数据库,以便重定向 ulrs 的项目可以使用它们。
我需要逻辑思想的是如何创建简短的随机字母。我只允许使用美国字母表中的数字和字母。还;
- 它应该使第一个字符成为数字,第二个字符成为字母。休息可以是任何事情。我如何实现该功能,以便框架可以为我创建随机别名。
- 首先,它将创建 4 个字符长的单词。但是如果没有 4 个字符长的未使用单词了怎么办?我该如何实现该功能?如果没有剩下唯一的 4 个字符长的单词,它应该创建 5 个字符长的单词,但它应该使第一个字符再次成为数字,第二个字符成为字母。休息也可以是任何事情。
我知道我将在 mscorlib.dll 中使用 System.Random 类,但说实话,我对此了解不多。更详细地说,我不知道如何使用数字和美国字母表中的字母创建随机唯一单词。
I need some ideas how to implement a URL Shortener Service with WCF along with sql server as database.
I will handle my url shortening logic with WCF. this project will handle creating short aliases for the urls and write them on the database with the actual url so that the project which will redirect the ulrs could use them.
where I need the logic ideas is on how to create short random letters. I will only allow numerical digits and letters in american alphabetic. Also;
- it should make the first char a numerical digit and the second char a letter. rest could be anything. how can I implement that feature so that framework could create random aliases for me.
- firstly, it will create 4 chars long words. but what if there are no 4 chars long unused words left? how can I implement that feature? if there are no unique 4 chars long words left, it should create 5 chars long words but it should make the first char a numerical digit again and the second char a letter. rest could be anything on this, too.
I know that I will be using System.Random class inside the mscorlib.dll but honestly, do not know much about it. More detailedly, I do not have any idea how to create a random unique word with numerical digit and letters in american alphabetic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要需要随机函数。随机性只会给你碰撞的机会。
只需使用递增的数字密钥并对其进行编码即可。您的数据库已经提供了创建它们的方法。
简单的编码是 Hex(基数 16),但使用基数 32(或更高)编码可以变得更短、更漂亮。我不确定“第一个字符应该是数字”的要求是否有用,但很容易实现。
虽然可逆编码似乎合乎逻辑,但将生成的编码存储为数据库中的列(键)并使用它进行查找也是相当可行的。这允许更奇特的编码,甚至在前面添加一个(随机)数字。
You do not need a Random function. Randomness only gives you a chance for collisions.
Simply use an incrementing, numerical, key and encode it. Your database already provides a way to create them.
A simple encoding would be Hex (base 16) but you can get shorter and fancier with a base 32 (or higher) encoding. I'm not sure if the requirement for 'first char should be numerical' is useful but it's easy to accomplish.
And while a reversible encoding seems logical, it's also quite feasible to store the generated encoding as a column (Key) in the database and use that for lookup. That allows more fancy encoding, even adding a (random) digit in front.
Sapph 发布的 URL 有一些很好的背景阅读内容。关于您的 WCF/SQL 组合。显然,您需要将数据放在某个地方,因此 SQL Server 是最好的选择。对于 WCF,大多数 URL 收缩服务都使用一个很好的简单的可破解 URL 结构。这意味着您可以通过 JavaScript 调用它并获取 JSON 结果以及呈现 HTML。
在这种情况下,您可以使用 ASP.NET MVC 或 WCF 托管服务。我可能会选择 MVC,只是因为它更容易,而且无论如何您可能都需要某种 UI。
The URL that Sapph posted has some good background reading. With regards to your WCF/SQL combination. Obviously you need to put your data somewhere, so SQL Server is as good as anything. As for WCF, most of the URL shrinking services use a nice simple hackable URL structure. This means that you could potentially call it via JavaScript and get a JSON result as well as rendering HTML.
Given that scenario you could use ASP.NET MVC or a WCF hosted service. I'd probably go with MVC simply because it'd be easier and you'll probably need some kind of UI anyway.