<< Click to Display Table of Contents >> URL amigáveis |
![]() ![]() ![]() |
Introdução
Em alguns sites a url ao invés de ser algo do tipo:
http://adrenaline.uol.com.br/mobile/noticias.aspx?noticia_id=18633
São:
http://adrenaline.uol.com.br/noticias/18633/foto-de-gamepad-da-logitech-para-iphone-vaza-na-internet
Como fazer?
Default.aspx
Crie uma página com 3 links observe que o destino tem um "id" / "titulo qualquer"
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="URLAMIGA.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:HyperLink ID="link1" runat="server" NavigateUrl="~/noticias/1/titulo-da-noticia-numero-um">Notícia n.1</asp:HyperLink><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/noticias/2/esse-cabra-se-ferrou">Notícia n.2 - cabra bom</asp:HyperLink><br />
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/noticias/3/estes-titulos-legais-o-cara-monta-da-base">Notícia n.3 (titulo legal)</asp:HyperLink><br />
</form>
</body>
</html>
Tela
Noticias.aspx
Nesta página recebemos um id e uma categoria por querystring e colocamos em labels para visualização
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Noticias.aspx.cs" Inherits="URLAMIGA.Noticias" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
body
{
font-family: Arial;
}
.red
{
color: red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>
<asp:Label ID="Label1" runat="server">Idenficador do registro: </asp:Label><asp:Label ID="lbID" CssClass="red" runat="server"></asp:Label>
</h3>
<h3>
<asp:Label ID="Label2" runat="server">Categoria: </asp:Label><asp:Label ID="lbCat" CssClass="red" runat="server"></asp:Label>
</h3>
</form>
</body>
</html>
Tela (depois de pronto)
Noticias.cs
using System;
public partial class Noticias : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbID.Text = Request.QueryString["id"];
lbCat.Text = Request.QueryString["categoria"];
}
}
Global.asax
Aqui está o pulo do gato, ele filtra os requests, se bate com uma expressão regular que
indica: texto/codigo/texto livre sem espaços e simbolos
using System;
using System.Text.RegularExpressions;
using System.Web;
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
string oldpath = incoming.Request.Path.ToLower();
// usa expressão regular para ver em que categoria entra
// w = letras, dígitos, ou '_'
// d = dígitos, de 0 a 9
// S = Negação de s aceita o que não for espaço em branco, \n \r ou \t
// + = o elemento precedente uma ou mais vezes. Por exemplo, ba+ casa "ba", "baa", "baaa", e assim por diante
Regex regex = new Regex(@"(\w+)/(\d+)/(\S+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = regex.Matches(oldpath);
// fechou ?
if (matches.Count > 0)
{
// Extrai o id da noticia e envia para Noticias.aspx
string id = matches[0].Groups[2].ToString();
string categoria = matches[0].Groups[1].ToString();
incoming.RewritePath(string.Format("~/Noticias.aspx?id={0}&categoria={1}", id, categoria));
}
}
}