3

If a website with url "www.site.com", redirects to HomeController's Index action.

I have

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

I want the url

www.site.com/{nick}

do the same thing

How I do to create a route and where do I have to create it?

In the RouteConfig.cs or in the AreaRegistration.cs?

4

2 回答 2

4

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

routes.MapRoute
(
    name: "Nick Route",
    url: "{nick}",
    defaults: new { area = "AreaName",
                    controller = "controllerName",
                    action = "Actionname",
                    nick = UrlParameter.Optional  });
于 2013-06-12T15:36:30.620 回答
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 }
        );
于 2014-08-27T12:23:47.510 回答