C# 如何将我的web表单url从“更改为”;页面/游戏.aspx?页面=1“;至;游戏/1“;?

C# 如何将我的web表单url从“更改为”;页面/游戏.aspx?页面=1“;至;游戏/1“;?,c#,asp.net,.net,webforms,routing,C#,Asp.net,.net,Webforms,Routing,我使用VS2017,我有简单的项目结构 使用老式的web表单,我试图为我的游戏.aspx添加简单的路由。据我所知,它看起来是这样的。只需在App\u code文件夹中创建新的RouteConfig类(这是我定义路由的地方) 并在Global.asaxStart方法中调用该静态方法 public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, E

我使用VS2017,我有简单的项目结构

使用老式的web表单,我试图为我的
游戏.aspx
添加简单的路由。据我所知,它看起来是这样的。只需在
App\u code
文件夹中创建新的
RouteConfig
类(这是我定义路由的地方)

并在
Global.asax
Start
方法中调用该静态方法

    public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}
但当我启动项目时,我发现了一个“未找到”错误:


当我直接调用我的
Games.aspx
时,它就像
Pages/Games.aspx?page=1
一样工作,但我不想在我的url中使用这些东西。我试图调试Start方法,但它似乎是由IIS或其他什么工具编译的。那么,我错在哪里?

我无法复制您的代码,因为将
RouteConfig
类放在
App\u code
中不允许我在
Global.asmx
中引用它。我所做的是将映射路由的方法放在我的
Gloabal.asmx
中,现在看起来是这样的

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
       RegisterRoutes(RouteTable.Routes);
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("", "games/{page}", "~/Games.aspx");
    }
}
每当我注释掉
RegisterRoutes()
方法时,我都会得到相同的错误。尝试这种方法,如果有帮助,请告诉我。
此外,要在
Game.aspx
中获取传递的值,您应该使用此
Page.RouteData.Values[“Page”]
您可以使用URL重写模块处理此问题:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="RewriteGames" stopProcessing="true">
                <match url="^games/(\d+)" />
                <action type="Rewrite" url="Pages/Games.aspx?page={R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>


正如@a.bajorinas所回避的那样,如果在路由中捕获
页面
参数,则必须修改代码以使用
页面.RouteData
而不是
请求.QueryString
。这真的不是什么大变化。但是,如果您想简单地更改URL,在web.config中使用下面的代码可能是最简单的方法。

此链接可能会帮助您@NabeelKhan,对于这样一个简单的任务来说,它有点太复杂了。routes.MapPageRoute(“,”games/{page},“~/games aspx”);您是否丢失了这里的页面,如routes.MapPageRoute(“,”games/{page}“,“~/pages/games.aspx”)@塞哈克斯,没有。事实并非如此。我甚至试过整条路。那没用。我试过了,不幸的是没用。但是谢谢你的方法。我认为这是将路由更改为
routes.MapPageRoute(“,”games/{page}“,“~/Pages/games.aspx”)
并使用
Page.RouteData.Values[“Page”]
。谢谢,严格来说,这不是我想要的,但它做到了。
<system.webServer>
    <rewrite>
        <rules>
            <rule name="RewriteGames" stopProcessing="true">
                <match url="^games/(\d+)" />
                <action type="Rewrite" url="Pages/Games.aspx?page={R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>