大家好我在url rewriting
上看过很多文章,但根据我的要求,我找不到任何文章。假设我有两个页面Default.aspx
和Default1.aspx
..在初始加载时,我想将Default.aspx
重写一些内容,例如urlrewrite\dummy.aspx
和Default.aspx
当我点击这个时我会有一个按钮我将重定向到Default1.aspx
我想将其重写为urlrewrite\dummy1.aspx
我只是发布了示例重写但是如果有更好的重定向方法,请帮助我..
如果我有一些20-50
页
我的global.asax文件
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Routing" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
string root = Server.MapPath("~");
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(root);
System.IO.FileInfo[] files = info.GetFiles("*.aspx", System.IO.SearchOption.AllDirectories);
foreach (System.IO.FileInfo fi in files)
{
string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
routeCollection.MapPageRoute(fi.Name + "Route", fi.Name, pageName);
}
routeCollection.MapPageRoute("DummyRouteName1", "Dummy", "~/Default2.aspx");
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
答案 0 :(得分:3)
您可以在应用程序启动时在Global.asax文件中添加路由:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("DummyRouteName", "Dummy", "~/Default.aspx");
....
}
用法:
Response.Redirect("~/Dummy");
在网址中,您会看到:(服务器)/ Dummy
修改强>
以下是如何自动添加路线:
// Get root directory
string root = Server.MapPath("~");
DirectoryInfo info = new DirectoryInfo(root);
// Get all aspx files
FileInfo[] files = info.GetFiles("*.aspx", SearchOption.AllDirectories);
foreach (FileInfo fi in files)
{
// Get relative path
string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
// Add route
routes.MapPageRoute(fi.Name + "Route", fi.Name.Replace(".aspx", ""), pageName);
}
答案 1 :(得分:0)
我假设您覆盖了重写部分且只有问题是回发,您可以通过设置表单操作将回发设置为“友好”URL,如下所示:
Page.Form.Action = Page.Request.RawUrl;