4

I'm a bit confused on which way to accomplish things. My website can display a Feed of stories to a user, and the feed can be one of multiple categories. (eg. you could look at a "All Stories" feed, or a "My Submissions" feed).

In terms of handling routing, does it make more sense to:

1) Have an Action (Home/index) handle different "storyCategory" parameters with routing like this:

[Route("~/"), Route("")] //Index will be default route for both SiteRoot and SiteRoot/Home
[Route("{storyCategory?}/{page?}")]
[Route("{storyCategory?}/Page/{page?}")]
public ActionResult Index(Story.Category storyCategory = Story.Category.None, int page = 1)

OR

2) have a specific action for each storyCategory instead of passing the enum in as a parameter:

[Route("~/"), Route("")] //Index will be default route for both SiteRoot and SiteRoot/Home
public ActionResult Index(int page = 1) 
public ActionResult ReadLater(int page = 1) 
public ActionResult PlanToUse(int page = 1)

2 Answers 2

2

If all your feeds are exactly like each other, with just a few actions where the parameters are always the same, the first option seems obvious...

However, if in the future you want to have, for example, a different "ReadLater" in one of the feeds (with different parameters), you may regret to have taken the first option.

I would take the second option for these reasons:

  • actions flexibility;
  • URLs are defined by action names (not hardcoded on top of your "index" action);
  • parameters can easily be adjusted according to the controller's context;
  • code's readability and maintainability.

Furthermore, if your feeds grow more than you expected, you can create a constants file where you correlate each controller and its actions in this way:

namespace Stories
{
    public class ControllersNames {
        public const string AllStories = "AllStories";
        public const string MySubmissions = "MySubmissions";
    }

    public class ActionsNames
    {
        #region AllStories
        public const string AllStories_ReadLater = "ReadLater";
        public const string AllStories_PlanToUse = "PlanToUse";
        #endregion

        #region MySubmissions
        public const string MySubmissions_ReadLater = "ReadLater";
        public const string MySubmissions_PlanToUse = "PlanToUse";
        //same action but with different paramaters below
        public const string MySubmissions_PlanToReUse = "PlanToUse"; 
        public const string MySubmissions_Store = "Store";
        #endregion
    }
}

And somewhere in your view you could have calls similar to this:

<a ... href="@Url.Action(
      ActionsNames.MySubmissions_PlanToUse,
      ControllersNames.MySubmissions,
      new { page = Model.MySubmissions.IDPage })">

It has been easier to read and follow up with more actions...

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

Comments

1

I would go with the first option as it makes little sense to make different actions just for filtering articles/content.

Also using enums in the route doesn't seem to be the perfect choice. Meaningful strings are better.

3 Comments

Why do you say that enums would be bad? Strings would have to be parsed
@Michael, enums are essential just integers so if you use them directly in the URI, you'll end up with a URI full of meaningless numbers and that's not good. You could do a ToString() on enums to get their identifying string but people say that hurts performance.
I have it set up for enums. While you are right, they are just integers, If the string value is typed into the url, it works. So it seems that there is an option to either use ints or the enum name. It does however, present a problem when user's potentially enter an integer that does not map to a valid enum value. Still a using a string, would mean parsing the strings, which seems to be an equivalent trade off to obtaining the string value of an enum

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.