ASP.NET routing in WebForms does not handle non-AS

2019-03-02 15:21发布

I've created a default WebSite under Visual Studio 2010. Added a simple routing there into Global.asax:

routes.MapPageRoute("AboutRoute", "about", "~/About.aspx");

This shows 404 when I start ASP.NET Development Server and browse to "http://localhost:6521/WebSite1/about"

But works nicely when I change about to about.axd (notice the .axd extension) and browse to /WebSite1/about.axd

What do I need to change in web.config to make Development Server work as IIS does (correctly handles extension-less URLs)?

1条回答
冷血范
2楼-- · 2019-03-02 15:46

This shows 404 when I start ASP.NET Development Server and browse to "http://localhost:6521/WebSite1/about"

I prepared a sample Web Application, which gave me About us and Default.aspx pages. In default.aspx page, I wrote the following code....

Default.aspx.cs Code

protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect(Page.GetRouteUrl("AboutRoute", 
                        new { ID = "Evgenyt" }));
}

Global.asax.cs Code

public class Global : System.Web.HttpApplication
{
    private void RegisterRoute(RouteCollection Routes)
    {
        Routes.MapPageRoute("AboutRoute", "about/{ID}", "~/About.aspx");

    }

    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoute(RouteTable.Routes);
        // Code that runs on application startup

    }
}

Then I published the code and configured it on IIS. Now When the Request Reaches IIS, It routed the message to the aspnet_isapi.dll ISAPI extension. ISAPI extension then loaded the default.aspx page, execute it, and return its rendered HTML to IIS, and finally IIS then sends it back to the client.

Resultant URL

http://localhost/Demo/about/Evgenyt

Actual URL

http://localhost/Demo/AboutUs.aspx


What do I need to change in web.config to make Development Server work as IIS does (correctly handles extension-less URLs)?

Reference - Unlike URLMapping, URLRouting does not take place in Web.config. It can be implemented using code. You can use Application_Start Event as mentioned in your code in Glogal.asax.cs file to register all routes for your application. To register a route you can use RouteTable class in System.Web.Routing namespace.

查看更多
登录 后发表回答