如何在WebForm ASP.Net中模拟MVC路由?

问题描述 投票:0回答:1

你们都已经看到MVC如何默认以URL形式“ {controller} / {action} / {id}”来最小化URL。在RouteConfig.cs中完成。

[我正在寻找一种方法,以便将mywebsite.com/Page/Default.aspx?id=100&Browser=ff之类的网络表单URL更改为mywebsite.com/Page/Default/100?Browser=ff,应在Globa.ascx中完成。

[StackOverFlow网站上有一些帖子,指示如何将保留的URL重定向到某个页面,很明显,我的问题是另外一回事,我正在寻找一种在Global.ascx中提供模式的方法。

asp.net webforms routing
1个回答
0
投票

在解决方案资源管理器中,在您的项目下,添加一个新的ASP.NET项目“ Global.asax”

添加using语句:

using System.Web.Routing;

在Application_Start事件中,输入您的路由URL,例如:

public class Global : System.Web.HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapPageRoute("default", "Page/Default/{controller}/{action}/{id}", "~/Page/Default.aspx");
    }

然后,在页面加载事件:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string member = RouteData.Values["controller"] + "";
        string action = RouteData.Values["action"] + "";
        string id = RouteData.Values["id"] + "";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.