2

I have one site and the controller is a name of the user like /John/NewPage, same thing for Mary.

How can I do this without having to create a controller for each new user?

Thks

2
  • 3
    Take a look at MVC Routing Commented Dec 3, 2012 at 19:55
  • You'll want to use routing like the others are saying. You will wind up using the same controller for every user, but you'll be able to achieve your desired URLs. Then, just use the username from the URL to lookup data from your db and you are set. Commented Dec 3, 2012 at 21:24

2 Answers 2

1

Look into custom routing. Something like:

    context.MapRoute(
        "User_default", // Route name
        "{userName}/{controller}/{action}/{id}", // URL with parameters
        new { id = UrlParameter.Optional } // Parameter defaults
    )

But you will have to take care of forming these url's yourself.

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

Comments

0

You could do something like this. Define a controller named something like "UserController" then use custom routes so that it isn't necessary to have "UserController" in the URL:

context.MapRoute(
    "User_Route", // Route name
    "{userName}/{action}", // URL with parameters
    new { 
      controller = "UserController",
      action = "NewPage"
    } // Parameter defaults
);

With that setup, the "/Mary/NewPage" URL would call UserController.NewPage("Mary"), "/John/EditPage" would call UserController.EditPage("John"), and just "/Jane" would call UserController.NewPage("Jane").

You would also need to define routes for different controllers above that route so they would get called first. For example:

context.MapRoute(
    "SomeOther_Route", // Route name
    "SomeOtherController/{action}/{id}", // URL with parameters
    new { 
      controller = "SomeOtherController"
    } // Parameter defaults
);

The above URLs will still work, but now this URL "/SomeOtherController/DefinedAction/12" will call something like SomeOtherController.DefinedAction(12).

Comments

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.