Pregunta

Quiero crear páginas dinámicas ASP.NET 4.0 cargadas desde mi servidor MS SQL. Básicamente, es una lista de ubicaciones con información. Por ejemplo:

Location1 would have the page www.site.com/location/location1.aspx
Location44 would have the page www.site.com/location/location44.aspx

Ni siquiera sé por dónde empezar con esto, ¿URL reescribe tal vez?

¿Fue útil?

Solución

La reescritura de URL aborda un problema diferente al que está describiendo.

Puede usar un httphandler que maneja las solicitudes en la ruta location y analice el último segmento para obtener su clave de búsqueda y simplemente pase la ejecución a un .aspx. Aunque está pasando la ejecución a una página general, la URL permanecerá como se ingresó.

Proporcionaré un ejemplo. Dale un tiro. Aquí hay un proyecto de muestra

UbicationHandler.cs

using System.IO;
using System.Web;

namespace DBHandler
{
    public class LocationHandler : IHttpHandler
    {
        #region IHttpHandler Members

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            string page = Path.GetFileNameWithoutExtension(request.Url.Segments[request.Url.Segments.Length - 1]);

            // for url ~/location/location33.aspx page will be 'location33'

            // do something interesting with page, perhaps 
            context.Server.Execute("~/locations.aspx?locationId=" + context.Server.UrlEncode(page));
        }

        public bool IsReusable
        {
            get { return false; }
        }

        #endregion
    }
}

ubicaciones.aspx

<%@ Page Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Request["locationId"];
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Web.Config Extracto

...
<system.web>
  ...
  <httpHandlers>
    <add verb="*" path="location/*.*" type="DBHandler.LocationHandler"/>
  </httpHandlers>
</system.web>
...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top