2011-06-10 46 views
0

我想在我的asp.net應用程序中使用簡短的beaty鏈接進行referal註冊。類似於http://MySite.ru/xbsdakj。我如何實現這個東西?在asp.net中的短鏈接

+0

什麼是推薦註冊 - ASP.NET的會員提供商的東西的一部分(即,你想幫助挖掘)或只是你自己的代碼?你有一個域和一個允許自定義域的URL shortener服務(例如[bit.ly pro](http://bit.ly/pro/products) - 我敢肯定還有很多其他的)? – Rup 2011-06-10 08:44:27

回答

0

如果你不使用MVC .NET和使用Web表單,你將不得不執行URL重寫。請參考http://msdn.microsoft.com/en-us/library/ms972974.aspx

+0

並不完全是一個完整的道理 - 路線確實在3.5+ – 2011-06-10 08:52:59

+0

中可用,您應該將其作爲答案發布。 – Tsar 2011-06-10 09:01:03

+0

完成 - 但沒有測試過對不起。 – 2011-06-10 13:34:29

0

你瞄準哪個ASP.net的版本提供這個的嗎?

版本3.5及以上的,你可以利用低於路由(即使有web表單),你不能:)

假設你可以使用的路線,你可以在全球ASAX以下...

protected void Application_Start(object sender,EventArgs e){RegisterRoutes(RouteTable.Routes); }

public static void RegisterRoutes(RouteCollection routes){routes.Add(new Route(「/ {hash}」,new HashRouteHandler())); }

其中HashRouteHandler就像是在這裏

using System.Web.Compilation; 
using System.Web.UI; 
using System.Web; 
using System.Web.Routing; 

public class HashRouteHandler: IRouteHandler 
{ 
    public CustomRouteHandler(string virtualPath) 
    { 
     this.VirtualPath = virtualPath; 
    } 

    public string VirtualPath { get; private set; } 

    public IHttpHandler GetHttpHandler(RequestContext 
      requestContext) 
    { 
     // Lookup URL from hash, e.g. mysite.ru/categories/signup?someinfo=jimmy%20dean 
     string querystring = LookupBasedOnHash(requestContext.RouteData.Values["hash"]); 
     HttpContext.Current.RewritePath(
      string.Concat(
      VirtualPath, 
      queryString)); 

     var page = BuildManager.CreateInstanceFromVirtualPath 
      (querystring , typeof(Page)) as IHttpHandler; 
     return page; 
    } 

答案中描述的處理程序}

0

前提

1)建立一個公共的API密鑰。如果你不知道如何閱讀這個。 Google Developer Console Create Public API Key 2)在Visual Studio中創建一個新項目並安裝以下NuGet包。 Install-Package Google.Apis.Urlshortener.v1

Google URL shortener API是一個公共API,這意味着我們不需要擔心身份驗證,我們可以使用公共API密鑰直接訪問它。

UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer() 
      { 
       ApiKey = "API KEY from Google developer console", 
       ApplicationName = "Daimto URL shortener Sample", 
      }); 

我們現在有一個我們可以用來訪問api的URLshortenerService。 縮短的URL

縮短使用谷歌URL縮短的URL是相當兩岸正向我們所說的插入方法。這將爲我們創建一個新的縮短的網址。縮短的網址可以在Id屬性中找到。

public static string shortenIt(string url) 
     { 
      UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer() 
      { 
       ApiKey = "API KEY from Google developer console", 
       ApplicationName = "Daimto URL shortener Sample", 
      }); 

      var m = new Google.Apis.Urlshortener.v1.Data.Url(); 
      m.LongUrl = url; 
      return service.Url.Insert(m).Execute().Id; 
     }