如何将url缩短为action参数

时间:2013-06-12 15:32:31

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

如果网站上有网址“www.site.com”,请重定向到HomeController的索引操作。

我有

www.site.com/area/controller/action/{nick}

我想要网址

www.site.com/{nick}

做同样的事情

如何创建路线以及在何处创建路线?

在RouteConfig.cs或AreaRegistration.cs?

2 个答案:

答案 0 :(得分:4)

RouteConfig.cs中,在所有路线后添加以下路线。

routes.MapRoute
(
    name: "Nick Route",
    url: "{nick}",
    defaults: new { area = "AreaName",
                    controller = "controllerName",
                    action = "Actionname",
                    nick = UrlParameter.Optional  });

答案 1 :(得分:2)

我知道这篇文章很老但如果有人有兴趣,他可以在这里找到解决方案http://www.nikolay-angelov.eu/custom-url-shortener-in-asp-net-mvc-5/

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

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

        routes.MapRoute(
            name: "URL",
            url: "{shortURL}",
            defaults: new { controller = "Home", action = "RedirectToLong", shortUrl = UrlParameter.Optional }
        );

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