Domanda

How to display custom url and not based on controller and action names?

My routing looks like

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
        name: "productfeature",
        url: "cn/pendrive/18-gb",
        defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
       );
    }
}

and my link button looks like

<li>@Html.RouteLink("Product Feature", "productfeature")</li>

I get url as the Url mentioned in the code but i also 404 error.

http://localhost:9090/cn/pendrive/18-gb

How to go for static urls? Basically my aim is to no show controllers and action names in url.

È stato utile?

Soluzione

Put your custom route before the default one:

public static void RegisterRoutes(RouteCollection routes)
{

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    name: "productfeature",
    url: "cn/pendrive/18-gb",
    defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
   );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );


}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top