2008-10-21 59 views

回答

25

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

+0

但为什么隐藏aspx扩展? – 2008-10-28 13:15:38

3

您可以使用ISAPI重写(对于IIS)执行此操作和更多操作。它允许你创建友好的网址,而不需要所有丑陋的查询字符串。它为用户提供了一个更友好的界面,可以让您的内容更加可搜索。

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

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

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

16

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

9

最有可能通过其URL重写做...

的Web服务器正在网址就像在你的浏览器&器的地址栏中的那些他们repointing到场景

这可能是背后的ASPX页面在.NET HTTP模块或一个ISAPI处理程序中完成的IIS

斯科特Gutherie在他的关于URL的网站一个很好的文章改写

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

+0

您确定“干净”的URL可以通过ASP.NET中的URL重写来完成。然而,在这种特殊情况下(stackoverflow.com),URL是由ASP.NET MVC框架的本质完成的。 – 2008-10-22 02:28:52

+0

...这就是所谓的“ASP.NET MVC的本质”在这种情况下是System.Web.Routing。 – 2009-01-29 00:36:25

-3

,并尽可能的理由:

  • 你可以改变技术(说PHP)无索引或书签的网址,打破
  • 您的网址更“REST'ful和对应的资源,而不仅仅是文件
  • 您可以记住的网址或通过电话读给别人更容易
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> 
0

下面的代码工作正常,只要在网页的.aspx,.ashx的是在应用程序文件夹。首先解决.aspx页面,然后是.ashx。

例如,如果您尝试使用localhost/AppFolder/Time,它会尝试解析localhost/AppFolder/Time.aspx,如果找不到,则使用localhost/AppFolder/Time.ashx。

P.S.

  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); 
       } 
      } 
     } 
    } 
} 
0

您可以在C#.NET这样做是为了用在ASP.NET您的网址自定义扩展。

让代码中的“.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"); 
    } 
}