ASP NET MVC Include Url Parameter on all pages

Asked

Viewed 3,494 times

6

I have an ASP NET MVC application that is multi-client and multi-user, IE, it is prepared to be used by multiple customers and each client can have multiple users.

Initially the login form has the text input for the client code. However I want to perform some routing for the user to inform the client code directly in the URL, I know that using the standard routing I can get this code taking the value of the parameter id in the controller, but this only works on the first page if some redirect is performed I can’t get this information.

How should I implement this? below my Routeconfig class:

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

            routes.MapRoute("Default", "{controller}/{action}/{id}",
                new {controller = "Acesso", action = "Login", id = UrlParameter.Optional});
        }
    }
  • 1

    Could you put some example? what I understood is that you would like to have some kind of mapping: {cliente}/{controller}/{action}/{id} where client is the customer ID for that section.

  • 1

    This is a very quiet implementation to do, just take care with @Julio security, where someone can go there and exchange this "id"

2 answers

4


Suppose you want a url in this pattern:

/empresaId/clientes/clienteId 

That could be translated to:

/1/clientes/10

You can create a route:

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

            routes.MapRoute("Empresa", "{empresa}/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional});

            routes.MapRoute("Default", "{controller}/{action}/{id}",
                new {controller = "Acesso", action = "Login", id = UrlParameter.Optional});
        }
    }

Set the company parameter in the controller:

public class Clientes : Controller 
{
    // rota: /1/clientes/10
    public ActionResult Index(int empresa, int id) 
    { 
    }

    // rota: /1/clientes/editar/10
    public ActionResult Editar(int empresa, int id) 
    { 
    }
}

Create a Session for the company id on login:

var empresaId = 1;
Session.Add("empresa", empresaId);

To then create the links, you make use of Session by passing the company parameter:

@Html.Action("index", "clientes", new { empresa = Session["empresa"] })

or

@Html.ActionLink("editar", "clientes", new { empresa = Session["empresa"] })

or

@Url.Action("index", "clientes", new { empresa = Session["empresa"] })
  • I would use a helper for those links =D just that to add

  • I would use a alias unique to customer face, so the customer url would be more user friendly, like: Empresaa/Usuarios/5.

1

I think something like this could solve your problem:

routes.MapRoute("Default", "Cliente/{id}",
            new {controller = "Cliente", action = "Pesquisar", id = UrlParameter.Optional});

Your controller would look like this:

public class Cliente : Controller 
{
    public ActionResult Pesquisar(int id) // id seria o código do cliente
    { ... }
}

The call would look like this:

localhost/Client/12345

  • Okay, @Gypsy, but this would be for the client page, what I need is then to pass this id to all the other pages I access from the client page, ie if I create an Actionlink for example, i want the target controller to come with the id parameter loaded

  • @Julioborges This has to be necessarily by route or could be using the Session context?

  • I believe that it can be through the context also, the iuristona illustrated the use of the context in a response, I believe that using the context of the session resolve. Thanks in advance.

  • @Julioborges How fast. I can hardly see your movements.

  • kkkk to here monitoring the issue and changing the classes. Vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.