【发布时间】:2010-09-18 10:26:54
【问题描述】:
我很确定 stackoverflow.com 是用 ASP.NET 创建的,但是无论我在哪里单击,地址栏中都看不到 .aspx 扩展名。 它是如何完成的,有什么特别的原因吗?
【问题讨论】:
标签: asp.net
我很确定 stackoverflow.com 是用 ASP.NET 创建的,但是无论我在哪里单击,地址栏中都看不到 .aspx 扩展名。 它是如何完成的,有什么特别的原因吗?
【问题讨论】:
标签: asp.net
在 stackoverflow 的情况下,他们使用 ASP.NET MVC 而不是 ASP.NET Web 表单。对于 Web 表单,url 指向磁盘上的文件,而 MVC 指向控制器操作。如果您使用的是网络表单,则需要使用 URL 重写。 Scott Guthrie 有一个good article 进行 URL 重写。
【讨论】:
此站点使用 ASP.NET MVC 框架和 Urls 映射到路由而不是物理页面。路由传递给控制器,然后控制器决定如何显示页面。
【讨论】:
很可能是通过 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.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
【讨论】:
您可以通过 ISAPI 重写(用于 IIS)来完成此操作以及更多操作。它允许您创建友好的 url,而无需所有丑陋的查询字符串。它为用户提供了更友好的界面,并使您的内容更易于搜索。
如果您使用的是 Apache,请使用 mod_rewrite。
两者的基本前提是它们采用友好的 url(就像您在本网站看到的那样),然后使用一系列规则(通常是您指定的正则表达式)将其转换为内部 url 或查询字符串代码很容易理解。
例如,他们使用转换规则将posts/edit/<postnumber> 转换为editPost.aspx?postNumber=<postnumber>。
【讨论】:
只要页面 .aspx、.ashx 位于应用程序文件夹中,下面的代码就可以正常工作。优先级是先解析.aspx页面,再解析.ashx。
例如如果你尝试 localhost/AppFolder/Time,它会尝试解析 localhost/AppFolder/Time.aspx,如果没有找到,则解析 localhost/AppFolder/Time.ashx。
附言
我没有完全测试这段代码,所以要小心。
它不考虑可能包含 .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);
}
}
}
}
}
【讨论】:
您可以在 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");
}
}
【讨论】:
就原因而言:
【讨论】: