1

大家好,我看过很多文章,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>
4

2 回答 2

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");

在 url 你会看到:(server)/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);
}
于 2012-05-11T11:04:48.750 回答
0

我假设您已经涵盖了重写部分,唯一的问题是回发,您可以通过设置表单操作将回发设置为“友好”URL,如下所示:

Page.Form.Action = Page.Request.RawUrl;
于 2012-05-11T11:19:04.140 回答