How does routing for ASP.NET Web Pages work without global.asax

安稳与你 提交于 2019-12-06 04:33:21

There are two kinds of "routing" available in the Web Pages framework. The default routing works on matching URLs to file paths. It is quite flexible in that it allows for additional URL segments that populate a UrlData dictionary, and can enable some nice SEO-friendly URL construction. I have written about that here: WebMatrix - URLs, UrlData and Routing for SEO.

The second kind of routing, which is similar to the routing available in MVC requires a package to be installed: Routing For Web Pages. Once you have installed this, you can either populate your RouteCollection in an _AppStart.cshtml file (which you need to create yourself) or you can do so in Application_Start in global.asax. You can add a global.asax file by selecting the All option in the Choose A File Type dialog when you choose to add a file.

If you want to know how to use the Routing package, I have written about that too: More Flexible Routing For ASP.NET Web Pages

Web Forms application

Global.asax

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Category/{action}/{categoryName}",
        "~/categoriespage.aspx");
}

MVC application

Global.asax

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

For more information :

How to: Use Routing with Web Forms

ASP.NET Routing not working on IIS 7.0

Deploying ASP.NET MVC 3 to IIS 6

IIS URL Rewriting and ASP.NET Routing

ASP.NET Routing

I hope this will help to you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!