3 using System.Collections.Generic;
4 using Nancy;
5
6 public class ShortUrlModule : NancyModule
7 {
8 private static readonly Dictionary<string, string> urlMap = new Dictionary<string, string>();
9
10 public ShortUrlModule()
11 {
12 Get["/"] = _ => View["index.html"];
13 Post["/"] = _ => ShortenUrl();
14 }
15
16 private string ShortenUrl()
17 {
18 string longUrl = Request.Form.url;
19 var shortUrl = ShortenUrl(longUrl);
20 urlMap[shortUrl] = longUrl;
21
22 return ShortenedUrlView(shortUrl);
23 }
24
25 private string ShortenUrl(string longUrl)
26 {
27 return "a" + longUrl.GetHashCode();
28 }
29
30 private string ShortenedUrlView(string shortUrl)
31 {
32 return string.Format("<a id=\"shorturl\" href=\"http://{0}/{1}\">;http://{0}/{1}</a>", Request.Headers.Host, shortUrl);
33 }
34 }
21 private Response ShortenUrl()
22 {
23 string longUrl = Request.Form.url;
24 var shortUrl = ShortenUrl(longUrl);
25 urlMap[shortUrl] = longUrl;
26
27 return View["shortened_url",
28 new { Host = Request.Headers.Host, ShortUrl = shortUrl }];
29 }
30
31 private string ShortenUrl(string longUrl)
32 {
33 return "a" + longUrl.GetHashCode();
34 }
35
"This post was aggregated from https://www.horsdal-consult.dk/2011/10/frictionless-net-web-app-development_16.html and all comments should be submitted on the original post"