0

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.

2
  • 3
    Since [USERNAME] can be any string value, you need to make sure the profile route is the last one being added to the routing table, to prevent hijacking all your prefedined routes. But still you need to wonder if you really want [USERNAME] to be a top level directory kind of route. What if someone has faq or rules as it's username? That can cause lot's of conflicts in your routes... Commented May 16, 2016 at 21:24
  • Thanks. Yes, it can be problem if a username is faq or rules. I can allways protect faq and rules from the username when the user create the account or i can change the address, www.page.com/faq -> www.page.com/start/faq. But for now, where should i start to send www.page.com/[USERNAME] to area www.page.com/Profile/[USERNAME] Commented May 17, 2016 at 10:36

1 Answer 1

0

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

}
Sign up to request clarification or add additional context in comments.

3 Comments

When i remove Profile, all requests is routing to /Profile/[USERNAME] even /faq & /rules.
You need to make sure the area registration happens after your regular route registration. Can you verify this? What is the order of execution in RouteConfig?
Yes, now it works. I changed the order, removed the default route in RegisterRoutes and add all pages(faq, rukes...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.