Webforms路由 - 具有不同页面的相同签名?

时间:2017-08-03 08:52:45

标签: asp.net webforms routing url-routing

我在一个既定的网站上工作。虽然很小(就页面而言),但有一些大的登陆页面以及通常的库存页面。

由于网站相对较小,因此页面结构不变。

https://example.com/contact
https://example.com/big-money-page

我们计划推出更多具有不同页面设计的页面。这意味着我们要么使用母版页和/或aspx模板化页面,要么创建我们自己的数据库驱动的CMS。

我可以看到的问题是网址:

模板类型1

Route url: /{Name} - e.g. /big-money-page
Physica path: ~/template1.aspx

模板类型2

Route url: /{Name} - e.g. /new-supporting-page
Physical path: ~/template2.aspx

我希望在不中断现有资金页面的情况下完成这项工作,并且如果可能的话,保留熟悉的网站结构,对于访问者来说,template1和template2是类似的页面,并且不会自然地存在于不同的文件夹中 - 他们的设计不同。

此外,修复深度路由文件夹结构使得将来很难进行更改。

我一直在使用WF路由一段时间,但总是以简单的方式。任何人都知道我如何才能使变更工作的后果有限?

更新---------------------------------------------- ----------------------

好的,在没有任何反馈意见的情况下,我想出了一个想法。我很欣赏有关可行性的反馈以及可以想到的任何缺点。

我的想法是拥有虚拟路线/页面。

路线的格式为http://example.com/{name}

虚拟页面使用placeholder {name}从目标页面的数据库中检索数据。

然后,我们使用从数据库中检索到的数据,将server.transfer转移到正确的目标页面。

我认为这会奏效,但我关注的是我不知道的事情:

  • server.transfer的浏览器兼容性
  • 性能开销
  • 对输出缓存的影响
  • 其他甚至没有想到的事情

当然,这不是一个理想的解决方案,但我也对任何其他想法持开放态度。

1 个答案:

答案 0 :(得分:2)

在WebForm项目中,可以使用自定义HTTPModule实现任务。实施包括几个步骤。简化版如下:
1。 SQL

create table dbo.UrlMap (
  publicUrl varchar(255) not null primary key,
  PhysUrl varchar(255) not null
)

在表格中填写一些数据,例如

publicUrl        PhysUrl 
big-money-page   template1.aspx?id=1
huge-money-page  template1.aspx?id=2
no-money-page    template2.aspx?id=3
other-page       template1.aspx?id=4

<强> 2。在App_code文件夹中创建一个类

using System;
using System.Web;

/// <summary>
/// Implements IHttpModule  with custom URLs
/// </summary>
public class UrlMap:IHttpModule
{
    /// <summary>
    /// Initialize the module 
    /// </summary>
    /// <param name="context"></param>
    void IHttpModule.Init(HttpApplication context)
    {
        context.BeginRequest += Context_BeginRequest;
        context.PostMapRequestHandler += Context_PostMapRequestHandler;
    }

    private void Context_BeginRequest(object sender, EventArgs e)
    {
        var app = (HttpApplication)sender;
        Url2PhysPath(app.Request.Path, app);
    }

    private void Context_PostMapRequestHandler(object sender, EventArgs e)
    {
        var app = (HttpApplication)sender;
        var pg = app.Context.Handler as System.Web.UI.Page;
        if (pg != null)
        {
            pg.PreRenderComplete += Pg_PreRenderComplete;
        }
    }

    private void Pg_PreRenderComplete(object sender, EventArgs e)
    {
        ProcessPageTree((System.Web.UI.Control)sender);
    }

    /// <summary>
    /// Replaces physical URLs on the page with "beutified" version
    /// </summary>
    /// <param name="control"></param>
    private void ProcessPageTree(System.Web.UI.Control control)
    {
        var form = control as System.Web.UI.HtmlControls.HtmlForm;
        if (form != null)
        {
            form.Action = BeautifyUrl(form.Page.Request.Url.PathAndQuery);
        }
        //other types in a similar way
        if (control.HasControls())
        {
            foreach(System.Web.UI.Control c in control.Controls)
            {
                ProcessPageTree(c);
            }
        }
    }
    /// <summary>
    /// Helper function. Can be inlined. 
    /// Searches "beautified" url in a DB and rewrites path
    /// </summary>
    /// <param name="url"></param>
    /// <param name="app"></param>
    private static void Url2PhysPath(string url, HttpApplication app)
    {
        using (var cnn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SiteCnn"].ConnectionString))
        {
            var cmd = new System.Data.SqlClient.SqlCommand("select physPath from dbo.urlMap where publicUrl=@url", cnn);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.Add("@url", System.Data.SqlDbType.VarChar, 255).Value = url;
            cnn.Open();
            string physPath = null;
            using(var r = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
            {
                if (r.Read())
                {
                    physPath = (string)r["physPath"];
                }
                r.Close();
            }
            if (!string.IsNullOrEmpty(physPath))
            {
                app.Context.RewritePath("/" + physPath);
            }
        }
    }

    /// <summary>
    /// Helper function 
    /// </summary>
    /// <param name="physUrl"></param>
    /// <returns>returns original url when nothing is found</returns>
    private static string BeautifyUrl(string physUrl)
    {
        using (var cnn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SiteCnn"].ConnectionString))
        {
            var cmd = new System.Data.SqlClient.SqlCommand("select publicUrl from dbo.urlMap where physPath=@url", cnn);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.Add("@url", System.Data.SqlDbType.VarChar, 255).Value = physUrl;
            cnn.Open();
            string pubUrl = physUrl;//to return original url when nothing is found
            using(var r = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
            {
                if (r.Read())
                {
                    pubUrl = (string)r["publicUrl"];
                }
                r.Close();
            }
            return pubUrl;
        }

    }
    /// <summary>
    /// Required by interface
    /// </summary>
    void IHttpModule.Dispose()
    {
     //   throw new NotImplementedException();
    }
}

第3。修改Web.config
注册模块。将以下行添加到configuration \ system.webServer \ modules

  <add name="UrlRewriter" type="UrlMap" preCondition="managedHandler"/>

跟进
server.transfer的浏览器兼容性不是问题。浏览器仅接收HTML
性能开销不多
对输出缓存的影响缓存优于template.aspx?id=123
其他甚至无法解决的事情 publicUrlphysUrl都必须是唯一的。实际上,您可以在静态Dictionary<string, string>变量中缓存直接和反向键查找。

相关问题