显示自定义网址?

时间:2014-02-21 12:57:09

标签: asp.net asp.net-mvc asp.net-mvc-4 url-routing

如何显示自定义网址而不是基于控制器和操作名称?

我的路由看起来像

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 }
       );
    }
}

,我的链接按钮看起来像

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

我获取了代码中提到的Url,但我也是404错误。

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

如何使用静态网址?基本上我的目标是在网址中不显示控制器和动作名称。

1 个答案:

答案 0 :(得分:2)

将自定义路线放在默认路线之前:

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 }
    );


}