【问题标题】:How do .NET sites hide .aspx extension of their files?.NET 站点如何隐藏其文件的 .aspx 扩展名?
【发布时间】:2010-09-18 10:26:54
【问题描述】:

我很确定 stackoverflow.com 是用 ASP.NET 创建的,但是无论我在哪里单击,地址栏中都看不到 .aspx 扩展名。 它是如何完成的,有什么特别的原因吗?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    在 stackoverflow 的情况下,他们使用 ASP.NET MVC 而不是 ASP.NET Web 表单。对于 Web 表单,url 指向磁盘上的文件,而 MVC 指向控制器操作。如果您使用的是网络表单,则需要使用 URL 重写。 Scott Guthrie 有一个good article 进行 URL 重写。

    【讨论】:

    • 但是为什么要隐藏 aspx 扩展呢?
    • 这是一个实现细节,没有特别的理由暴露给公共接口;没有它,或者'cgi',或者'.php',或者......
    • 这样页面是独立于语言的。
    【解决方案2】:

    此站点使用 ASP.NET MVC 框架和 Urls 映射到路由而不是物理页面。路由传递给控制器​​,然后控制器决定如何显示页面。

    【讨论】:

      【解决方案3】:

      很可能是通过 URL 重写完成的...

      网络服务器正在获取类似于浏览器地址栏中的 URL 并将它们重新指向幕后的 ASPX 页面

      这可以在 .NET HTTP 模块中完成,也可以作为 IIS 中的 ISAPI 处理程序完成

      Scott Gutherie 在他的网站上有一篇关于 URL 重写的好文章

      http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

      【讨论】:

      • 你说得对,“干净”的 URL 可以通过 ASP.NET 中的 URL 重写来完成。然而,在这种特殊情况下 (stackoverflow.com),URL 是由 ASP.NET MVC 框架的特性完成的。
      • ... 在这种情况下,所谓的“ASP.NET MVC 的本质”就是 System.Web.Routing。
      【解决方案4】:

      你可以通过修改你的web.config文件来实现。

      <configuration>
      <system.webserver>
      <rewrite>
         <rules>
                  <rule name="RemoveASPX" enabled="true" stopProcessing="true">
                      <match url="(.*)\.aspx" />
                      <action type="Redirect" url="{R:1}" />
                  </rule>
                  <rule name="AddASPX" enabled="true">
                      <match url=".*" negate="false" />
                      <conditions>
                          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                          <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
                      </conditions>
                      <action type="Rewrite" url="{R:0}.aspx" />
                  </rule>
         </rules>
      </rewrite>
      </system.webserver>
      </configuration>
      

      【讨论】:

      • 传递查询字符串时您的解决方案不起作用
      • @Thameem 你可以在正则表达式的帮助下解决它。
      【解决方案5】:

      正如其他人所回答的,StackOverflow 是使用 ASP.NET MVC 构建的,而 ASP.NET MVC 使用 System.Web.Routing。但是 System.Web.Routing 不是 ASP.NET MVC 的一部分,它是带有 SP1 的 RTMd,这意味着可以在没有 ASP.NET MVC 的情况下使用它。您可以在此处查看如何将其与 WebForms 一起使用:http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx 和此处:http://www.codeplex.com/ASPNET35Routing

      【讨论】:

        【解决方案6】:

        您可以通过 ISAPI 重写(用于 IIS)来完成此操作以及更多操作。它允许您创建友好的 url,而无需所有丑陋的查询字符串。它为用户提供了更友好的界面,并使您的内容更易于搜索。

        如果您使用的是 Apache,请使用 mod_rewrite。

        两者的基本前提是它们采用友好的 url(就像您在本网站看到的那样),然后使用一系列规则(通常是您指定的正则表达式)将其转换为内部 url 或查询字符串代码很容易理解。

        例如,他们使用转换规则将posts/edit/&lt;postnumber&gt; 转换为editPost.aspx?postNumber=&lt;postnumber&gt;

        【讨论】:

          【解决方案7】:

          只要页面 .aspx、.ashx 位于应用程序文件夹中,下面的代码就可以正常工作。优先级是先解析.aspx页面,再解析.ashx。

          例如如果你尝试 localhost/AppFolder/Time,它会尝试解析 localhost/AppFolder/Time.aspx,如果没有找到,则解析 localhost/AppFolder/Time.ashx。

          附言

          1. 我没有完全测试这段代码,所以要小心。

          2. 它不考虑可能包含 .aspx 文件的文件夹,因此,如果您尝试访问 /PhysicalApplicationPath/MYFOLDER/page,它不会解析为 /PhysicalApplicationPath/MYFOLDER/page.aspx。

          代码:

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Web;
          using System.Web.Security;
          using System.Web.SessionState;
          
          namespace NameSpace
          {
              public class Global : System.Web.HttpApplication
              {
                  private void mapearUrlAmigaveis()
                  {
                      String url = Request.Path.ToString().ToLower();
                      int positionQuestionMarkParameter = url.IndexOf('?');
          
                      String urlSemParametros = (positionQuestionMarkParameter != -1) ? url.Substring(0, (positionQuestionMarkParameter - 1)) : url;
                      String[] splitBarra = urlSemParametros.Split('/');
                      int indexOfUltimaBarra = urlSemParametros.LastIndexOf('/');
          
                      if (splitBarra.Length > 0)
                      {
                          String ultimaBarra = splitBarra[(splitBarra.Length - 1)];
                          String caminhoLocalUltimaBarra = Request.PhysicalApplicationPath + ultimaBarra;
                          String parametros = ((positionQuestionMarkParameter != -1) ? url.Substring((positionQuestionMarkParameter - 1), (url.Length - 1)) : String.Empty);
                          if (System.IO.File.Exists(caminhoLocalUltimaBarra + ".aspx"))
                          {
                              Context.RewritePath(urlSemParametros + ".aspx" + parametros);
                          }
                          else if (System.IO.File.Exists(caminhoLocalUltimaBarra + ".ashx"))
                          {
                              Context.RewritePath(urlSemParametros + ".ashx" + parametros);
                          }
                      }
                  }
              }
          }
          

          【讨论】:

            【解决方案8】:

            您可以在 c# .NET 中执行此操作,以便在 ASP.NET 中的 URL 中使用自定义扩展。

            让代码中的“.recon”成为您的自定义扩展。 (即将“.recon”替换为您自己的扩展名)

             protected void Application_BeginRequest(object sender, EventArgs e)
             {
                HttpApplication app = sender as HttpApplication;
                if (app.Request.Path.ToLower().IndexOf(".recon") > 0)
                {
                    string rawpath = app.Request.Path;
                    string path = rawpath.Substring(0, rawpath.IndexOf(".recon"));
                    app.Context.RewritePath(path+".aspx");
                }
             }
            

            【讨论】:

              【解决方案9】:

              就原因而言:

              • 您可以更改技术(比如 PHP),而不会破坏索引或书签 URL
              • 您的 URL 更加“REST”,并且对应于资源,而不仅仅是一个文件
              • 您可以更轻松地记住 URL 或通过电话将其读给其他人听

              【讨论】:

              • 为什么有这么多反对票?问题专门问为什么要这样做?!
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-11-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-03-29
              相关资源
              最近更新 更多