ASP.NET MVC 2 Routing with additional consistent parameters (not just the controller and action)

I currently have URLs that look like this:

http://www.example.com/user/create
http://www.example.com/user/edit/1

But now I have to support several organizations and their users. I need to have something like this:

http://www.example.com/org-name/user/create
http://www.example.com/org-name/user/edit/1

I had problems making the routes work fine, so I had to add a token at the beginning of the organization name so that the routing would not confuse it with the controller / action pair. Not a huge deal, but now my URLs look like this:

http://www.example.com/o/org-name/user/create
http://www.example.com/o/org-name/user/edit/1

It's good. I can live with it.

Here, where I encountered difficulties:
When I create URLs, as soon as I have an organization selected, it will not be saved in the name of the organization. So when I am here:

http://www.example.com/o/org-name

... Url.Action( "", "" ) URL-, :

/user/create

... , :

/o/org-name/user/create

( ):

        routes.MapRouteLowercase(
            "DefaultOrganization",
            "{token}/{organization}/{controller}/{action}/{id}",
            new { id = UrlParameter.Optional },
            new { token = "o" }
        );

        routes.MapRouteLowercase(
            "OrganizationDashboard",
            "{token}/{organization}/{controller}",
            new { controller = "Organization", action = "Dashboard" },
            new { token = "o" }
        );

        routes.MapRouteLowercase(
            "DefaultSansOrganization",
            "{controller}/{action}/{id}",
            new { controller = "Core", action="Dashboard", id = UrlParameter.Optional }
        );

ASP.NET MVC Custom Routing Long Custom Route, .

, , , .


:
Womp , ?

public static string ActionPrepend(this UrlHelper helper, string actionName, string controllerName)
    {
        string currentUrl = helper.RequestContext.RouteData.Values["url"] as string;
        string actionUrl = string.Empty;

        if (currentUrl != null)
        {
            Uri url = new Uri(currentUrl);

            if (url.Segments.Length > 2 && url.Segments[1] == "o/")
                actionUrl = string.Format("{0}{1}{2}{3}", url.Segments[0], url.Segments[1], url.Segments[2],
                    helper.Action(actionName, controllerName));
        }

        if(string.IsNullOrEmpty(actionUrl))
            actionUrl = helper.Action(actionName, controllerName);

        return actionUrl;
    }


:
, , . {} URL-. , - :

routes.MapRouteLowercase(
    "Organization",
    "{organization}/{controller}/{action}/{id}",
    new { controller = "Organization", action = "Dashboard", id = UrlParameter.Optional },
    new { organization = @"^(?!User|Account|Report).*$" }
);

routes.MapRouteLowercase(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Core", action = "Dashboard", id = UrlParameter.Optional }
);
+3
1

Url.Action URL- . , , , , -, , . Url.Action , "" "".

, โ€‹โ€‹ URL. :

Url.Action("User", "Create", new { token = "o", organization = "organization" })
+2

Source: https://habr.com/ru/post/1756252/


All Articles