简体   繁体   中英

Routing area in asp.net mvc

I have a profile site and now i want to make it easy to remember the url.

I have links like

  • www.page.com/faq
  • www.page.com/news/1
  • www.page.com/rules
  • www.page.com/profile/[USERNAME]

Now, i want to change the url, www.page.com/profile/[USERNAME] to be, www.page.com/[USERNAME]. The profile is a own area in my project.

My ProfileAreaRegistration.cs

    public override string AreaName => "Profile";

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Profile_media",
            "Profile/{ProfileName}/Media/{id}",
            new
            {
                area = "Profile",
                controller = "Media",
                action = "Index"
            },
            new[] { "MySite.Areas.Profile.Controllers" });

        context.MapRoute(
            "Profile_default",
            "Profile/{ProfileName}/{controller}/{action}/{id}",
            new
            {
                area = "Profile",
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            },
            new[] { "MySite.Areas.Profile.Controllers" });

    }

My RouteConfig.cs

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

        routes.MapRoute(
            "News", // Route name
            "News/{id}", // URL with parameters
            new {controller = "News", action = "Index", id = UrlParameter.Optional},
            new[] {"MySite.Controllers.News"}
            );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new {controller = "Home", action = "Index", id = UrlParameter.Optional},
            new[] {"MySite.Controllers"}
            );
    }

How can i get www.page.com/rules and www.page.com/[USERNAME] working? When i enter www.page.com/[USERNAME], the area Profile should be called.

Leave out Profile from the ProfileArea route, like this:

public override string AreaName => "Profile";

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Profile_media",
        "{ProfileName}/Media/{id}",
        new
        {
            area = "Profile",
            controller = "Media",
            action = "Index"
        },
        new[] { "MySite.Areas.Profile.Controllers" });

    context.MapRoute(
        "Profile_default",
        "{ProfileName}/{controller}/{action}/{id}",
        new
        {
            area = "Profile",
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        },
        new[] { "MySite.Areas.Profile.Controllers" });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM