【问题标题】:Alternative Query String C# (asp.net)替代查询字符串 C# (asp.net)
【发布时间】:2009-01-20 14:33:17
【问题描述】:

有没有一种快速而肮脏的方式来使用如下传递的查询:

domain.com/mypage.aspx/product/toycar/

我以前在 PHP 中做过,但这需要在页面中完成(在本例中)。

-- 我只能访问 aspx 页面和后面的代码,并且必须在 asp.net 2 中工作(我希望我使用的是 3.5)

【问题讨论】:

    标签: asp.net-2.0 c#-2.0 query-string


    【解决方案1】:

    又快又脏:

    public class ModuleRewriter : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        }
    
        private void Application_BeginRequest(Object source, EventArgs e)
        {
            // The url will look like: http://domain.com/mypage.aspx/product/toycar/ 
            // The module will rewrite it to: http://domain.com/mypage.aspx?product=toycar
    
            HttpApplication application = source as HttpApplication;
            string[] urlInfo = application.Request.RawUrl.ToString().Split('/');
            if (urlInfo.Length > 2)
            {
                string page = urlInfo[urlInfo.Length - 3];
                string action = urlInfo[urlInfo.Length - 2];
                string id = urlInfo[urlInfo.Length - 1];
                if (string.IsNullOrEmpty(page))
                {
                    page = "default.aspx";
                }
                application.Server.Transfer(string.Format(
                    "~/{0}?{1}={2}", page, action, id));
            }
        }
    
        public void Dispose()
        {
        }
    }
    

    web.config:

    <httpModules>
        <add name="ModuleRewriter" type="ModuleRewriter, MyWebApplication"/>
    </httpModules>
    

    还有一个测试页:

    <%@ Page Language="C#" %>
    <!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>
            <%= Request["product"] %>    
        </div>
        </form>
    </body>
    </html>
    

    【讨论】:

    • 不,您需要在某处注册模块。如果您不喜欢 web.config,您可以在 global.asax(在 Application_Start 事件中)以编程方式完成。
    • 获得 isapi-rewrite 时,我只是想知道是否可以使用 IIS/.net 实现与使用 Apache/PHP 设置相同的效果。显然我可以,只是不那么容易。谢谢。
    【解决方案2】:

    您可能想查看 ASP.NET System.Web.Routing 命名空间,我相信它是在 .NET 3.5 SP1 中添加的:

    http://blogs.msdn.com/mikeormond/archive/2008/05/14/using-asp-net-routing-independent-of-mvc.aspx

    http://msdn.microsoft.com/en-us/library/system.web.routing.aspx

    您也可以摆脱 .aspx 扩展名。

    【讨论】:

    • 谢谢,我已经阅读了 msdn 链接。不幸的是,我坚持使用 .net 2 并且只能访问 aspx 文件。
    【解决方案3】:

    这将涉及制作自定义 HTTP 处理程序。

    查看this

    【讨论】:

      【解决方案4】:

      您有几个选项,但所有选项都需要访问 web.config 并更改 IIS 以将所有文件扩展名映射到 dotNet ISAPI dll:

      1. 使用 MVC(就像 stackoverflow 一样, 注意网址)
      2. 使用 asp.net 路由(3.5 中的新功能)
      3. 编写自己的 http 处理程序 Massive guide from Microsoft here
      4. 使用出色的 urlrewriting.net,它几乎可以完美地完成所有事情,包括解决一些尴尬的身份验证和图像路径问题。

      我个人使用 urlrewriting.net 效果很好。

      由于您提到除了背后的代码和页面之外,您无权访问任何内容,我唯一能想到的实际上是创建这些目录(如果您有权这样做)并使用 server.transfer 页面将值传递到上面文件夹中的实际页面。杂乱无章,但如果您无法访问其他内容,您的选择将受到限制。

      【讨论】:

        【解决方案5】:

        如果您只想从 .aspx 中读取路径:

        Request.ServerVariables["PATH_INFO"]

        澄清一下:

        他只能访问aspx(+代码隐藏)本身,所以他必须知道查询是如何的,但由于格式原因,它不在Request.QueryString中。所以唯一的方法是 Request.ServerVariables["PATH_INFO"] (Request.RawUrl)

        【讨论】:

        • 谢谢你,很有用;但我希望当我们重写 isapi 时,我将不得不求助于它们。
        猜你喜欢
        • 2015-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多