.NET网站如何隐藏其文件的.aspx扩展名



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

在stackoverflow的情况下,它们使用ASP.NET MVC而不是ASP.NET web表单。对于web表单,url指向磁盘上的文件,而MVC指向控制器操作。如果你使用的是网络表单,你会想要使用URL重写。Scott Guthrie有一篇关于URL重写的好文章。

此网站使用ASP.NET MVC框架和URL映射来路由而非物理页面。路由传递给控制器,然后控制器决定如何显示页面。

很可能是由URL重写完成的。。。

网络服务器正在获取与浏览器地址栏中的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

您可以通过修改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>

正如其他人所回答的,StackOverflow是使用ASP.NET MVC构建的,ASP.NET MVC使用System.Web.Routing。但是System.Web.Rouging不是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

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

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

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

一个例子是他们通过使用转换规则将posts/edit/<postnumber>转换为editPost.aspx?postNumber=<postnumber>

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

例如,如果您尝试localhost/AppFolder/Time,它将尝试解析localhost/AppFolder/Time.aspx,如果未找到,则解析localhost/App Folder/Time.ashx.

p.S.

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

  2. 它不考虑可能包含.aspx文件的文件夹,因此,如果您尝试访问/PhysicalApplicationPath/MYFOLDER/page,它将不会解析为/Physical ApplicationPath/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);
                }
            }
        }
    }
}

您可以在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");
    }
 }

以及原因:

  • 您可以在不破坏索引或书签URL的情况下更改技术(比如PHP)
  • 您的URL更加"REST'",并且与资源相对应,而不仅仅是一个文件
  • 你可以更容易地记住URL或通过电话向某人读取

最新更新