mappageroute for mydomain/product to mydomain/prod

2019-09-02 00:37发布

问题:

At the moment Im getting a 404 error for any product page of the form mydomain/product

I would like to map these to mydomain/showproduct.aspx?pagename=product

Is it something like routes.MapPageRoute( "Product",
"{Prodname}",
"~/showproduct.aspx"
); Not sure if this will work and not sure about the querystring

回答1:

That URL you would like to map looks more like a classic ASP.NET URL then an MVC URL. In MVC you are not linking to physical files but action methods in a controller class.

In MVC it should look more like: mydomain/products/show/productname

The default URL route should handle the above URL structure:

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

EDIT:

If you have to do it the other way try something like this:

    routes.MapRoute(
   "Page",
   "{name}.aspx",
   new { controller = "Page", action = "Index", id = "" }
);