编写 Asp.net Web 表单应用程序 URL 就像 Asp.net MVC使用 Razor URL

问题描述

您好,请原谅我可能不重要的问题,我只是想重新装饰我在 Asp.net Web Forms 应用程序中使用的 URL,使其像使用 razor 的 Asp.net MVC 应用程序一样,这是我完成的第一步是删除 aspx 后继者,我已经完成了这个,但我正在尝试的第二步是用 pagename/id 替换 pagename?id=someid 或除通常公式之外的任何公式,任务最终是减少 ULR 中使用的字符,因此删除 .aspx 将排除 4 个字符,第二种方法将用斜杠替换 ?id= 并且使用 pagename+id 重命名页面(如 n1)不会命中服务器,因为页面名称已更改 欢迎任何建议! 我在我的 web.config 上做了以下更改以排除 aspx 后继

<system.webServer>
        <rewrite>
            <rules>
                <rule name="RewriteASPX">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}.aspx" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>

解决方法

更新!经过几个小时的搜索和测试,我能够找到半解决方案, 我将其称为半解决方案,因为 ajax 调用不再有效,并且任何具有更新面板的页面都显示与 axd 相关的错误,我使用路由作为下面的链接

ASP.NET Routing with Web Forms

刚刚将以下代码添加到 global.asax 文件中

protected void Application_Start(object sender,EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }
        void RegisterRoutes(RouteCollection routes)
        {
               routes.MapPageRoute(
                "mypage","mypage/{Name}","~/mypage.aspx"
                                  );
         }

在我的页面上,我重定向为:

Response.Redirect(GetRouteUrl("mypage",new { Name = "myparam" }));