Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:23:31+00:00 2026-05-13T20:23:31+00:00

I have a User entity, and in various views, I want to create links

  • 0

I have a User entity, and in various views, I want to create links to a user home page basically. This functionality should be available in different controllers, so I can easily redirect to the user’s home page. Each user in my site has a role ; for example reader, writer, editor, manager and admin. Ideally, I want to try to achieve something like this:

In a controller, for example

public ActionResult SomeThingHere() {
    return View(User.GetHomePage());
//OR 
    return RedirectToROute(User.GetHomePage());
}

in a View, I also want to use the same functionality, for example:

<%= Html.ActionLink("Link to home", user.GetHomePage() %>

Is it possible to achieve such a design in MVC? If so , how should I go about it?

I currently use a method like this, but it is only in one controller at the moment. Now I need to use the same code somewhere else and I am trying to figure out how I could refractor this and avoid repeating myself?

....
private ActionResult GetHomePage(User user){
    if (user.IsInRole(Role.Admin))
        return RedirectToAction("Index", "Home", new { area = "Admin" });

    if (user.IsInRole(Role.Editor))
        // Managers also go to editor home page
        return RedirectToAction("Index", "Home", new {area = "Editor"});

    if (user.IsInRole(Role.Reader))
        // Writer and reader share the same home page
        return RedirectToAction("Index", "Home", new { area = "Reader" });

    return RedirectToAction("Index", "Home");
}
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:23:32+00:00Added an answer on May 13, 2026 at 8:23 pm

    Well I finally came up with a design that seems to work. I have written an controller extension,
    with a GetHomePage Method. This extension can also be used in your views. Here is how I did It:

    public static class UserHelperExtension    {
        public static string GetHomePage(this ControllerBase controller, User user) {
            return = "http://" + controller.ControllerContext
                                .HttpContext.Request
                                .ServerVariables["HTTP_HOST"] + "/"
                               + GetHomePage(user);
        }
    
        //need this for views
        public static string GetHomePage(string httphost, User user) {
            return  = "http://" + httphost + "/" + GetHomePage(user});
        }
    
        private static string GetHomePage(User user) {
            if (user.IsInRole(Role.Admin))
                return "/Admin/Home/Index";
            if (user.IsInRole(Role.Editor))
                return "/Editor/Home/Index";
            if (user.IsInRole(Role.Reader))
                return "/Reader/Home/Index";
            return "/Home/Index";
        }
    }
    

    The action method in the controller looks like this:

        using Extensions;
    ...
    public ActionResult SomethingHere() {
        return Redirect(this.GetHomePage(user));
    }
    ...
    

    In the view I have this:

    ...
    <%@ Import Namespace="Extensions"%>
    <%=UserHelperExtension.GetHomePage(Request.ServerVariables["HTTP_HOST"], user)%>
    ...
    

    The advantage is that I can easily use this “GetHomePage” method in various controllers,
    or views thoughout my application, and the logic is in one place. The disadvantage is that
    I would have preferred to have it more type safe. For example, in my orignal tests, I had access to RouteValues collection:

    public void User_should_redirect_to_role_home(Role role,
        string area, string controller, string action) {
    ...
    var result = (RedirectToRouteResult)userController.SomeThingHere();
        Assert.That(result.RouteValues["area"], 
            Is.EqualTo(area).IgnoreCase);
        Assert.That(result.RouteValues["controller"], 
            Is.EqualTo(controller).IgnoreCase);
        Assert.That(result.RouteValues["action"], 
            Is.EqualTo(action).IgnoreCase);
    ...
    

    }

    But now that I am using a string so it is not type safe, and checking the RedirectResult.Url.

    ...
    var result = (RedirectResult) userController.SomethingHere();
    Assert.That(result.Url.EndsWith("/" + area + "/" + controller + "/" + action), 
        Is.True);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our application, I have seen code written like this: User.java (User entity) public
i have user control, which i render on several views. i want to show
Let's say I have an User entity and I would want to set it's
I'm using hibernate, I have a user entity, however I want it to implement
I have a User entity which represents a User table in my database. This
This is a Spring Security question. In my application, I have a User entity
I have a User entity that has a IList property. The mapping for this
I have two classes, user and role, defined as: public class User : Entity
I have a 'vanilla' install of CodeIgniter 2 + Doctrine 2. My tutorial Entity.User.dcm.yml
it's possible to have one entity object (class, e.g. User) for more entity models

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.