是否有一种快速而肮脏的方式来使用如下传递的查询:
domain.com/mypage.aspx/product/toycar/
我以前在 PHP 中做过,但这需要在页面中完成(在本例中)。
-- 我只能访问 aspx 页面和后面的代码,并且必须在 asp.net 2 中工作(我希望我使用的是 3.5)
是否有一种快速而肮脏的方式来使用如下传递的查询:
domain.com/mypage.aspx/product/toycar/
我以前在 PHP 中做过,但这需要在页面中完成(在本例中)。
-- 我只能访问 aspx 页面和后面的代码,并且必须在 asp.net 2 中工作(我希望我使用的是 3.5)
又快又脏:
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()
{
}
}
网络配置:
<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>
您可能想查看 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 扩展名。
这将涉及制作自定义 HTTP 处理程序。
检查这个
如果您只想从 .aspx 中读取路径:
Request.ServerVariables["PATH_INFO"]
澄清:
他只能访问aspx(+代码隐藏)本身,所以他必须知道查询是如何的,但由于格式原因,它不在Request.QueryString中。所以唯一的方法是 Request.ServerVariables["PATH_INFO"] (Request.RawUrl)
您有几个选项,但所有选项都需要访问 web.config 并更改 IIS 以将所有文件扩展名映射到 dotNet ISAPI dll:
我个人使用 urlrewriting.net 效果很好。
既然你提到除了背后的代码和页面之外你无权访问任何东西,我唯一能想到的就是创建这些目录(如果你有权这样做)并使用 server.transfer 页面传递值到您上面文件夹中的实际页面。凌乱,但如果你不能访问其他的东西,你的选择是有限的。