我使用VS 2017,我有一个简单的项目结构。

使用旧时尚的web表单,我尝试为我的Games.aspx添加简单的路由。根据MSDN的说法,情况会是这样的。只需在RouteConfig文件夹中创建新的App_Code类(这是我定义路由的地方)
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "games/{page}", "~/Games.aspx");
}
}并在Global.asax Start方法中调用该静态方法。
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}但是,当我启动该项目时,没有发现错误:

当我像Pages/Games.aspx?page=1一样直接调用它时,我的Pages/Games.aspx?page=1就能工作,但我不想让它出现在我的url中。我尝试过调试Start方法,但它似乎是由IIS或其他什么工具编译的。我哪里错了?
发布于 2018-10-30 18:43:31
您可能可以使用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无法捕获路由中的page参数,那么您必须修改代码以使用Page.RouteData而不是Request.QueryString。这并不是什么大变化。但是,如果您想简单地更改URL,使用下面的代码在web.config中这样做可能是最简单的。
发布于 2018-10-30 11:06:12
我无法复制您的代码,因为将RouteConfig类放在App_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"]
https://stackoverflow.com/questions/53034866
复制相似问题