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 8436285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:08:24+00:00 2026-06-10T07:08:24+00:00

Suppose I have a CategoriesController for managing some kind of categories list. It has

  • 0

Suppose I have a CategoriesController for managing some kind of categories list. It has these action methods:

public ActionResult Get(Int32 categoryId);

public ActionResult Edit(Int32 categoryId);

[HttpPost]
public ActionResult Edit(Int32 categoryId, CategoryModel model);

public ActionResult Delete(Int32 categoryId);

[HttpPost]
public ActionResult Delete(Int32 categoryId, DeleteModel model);

All of these methods run the same code that verifies that categoryId is valid and the category exists, it retrieves it from the database, and performs access-level verification. Parts of the verification code returns an ActionResult directly, others do not (for example, if the categoryId doesn’t exist, then it will return a Http404 action result immediately, but if everything’s okay then it goes down to the action-specific code.

Is the best way to cut down on code duplication to do something like this?

private ActionResult EnsureCategory(Int32 categoryId, out DBCategory dbCategory);

public ActionResult Edit(Int32 categoryId) {
    DBCategory dbCategory;
    ActionResult error = EnsureCategory(categoryId, out dbCategory);
    if( error != null ) return error;

    // this now means that only 3 lines of code will be shared between the Action methods, but it still seems too much.
}

It’s a shame C# doesn’t support preprocessor macros, or something like it, because this would be a good place to use them. Unless there’s a better approach?

  • 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-06-10T07:08:25+00:00Added an answer on June 10, 2026 at 7:08 am

    I ended up continuing to use the approach I suggested to myself in my original question. I did consider using @SteveB’s T4 template suggestion, but I find T4 templates a bit hard to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have a module which defines some basic constants such as integer, parameter
Suppose I have a mongodb collections A and B. While A has a foreign
suppose we have this next sample code: while(some condition){ lock1.lock(); . . } the
Suppose I have two pages: - page 1 uses jquery mobile and has a
Suppose I have this animation that start at some point : myElement.animate({ top: -currentTempImageX,
Suppose you have val docs = List(List(one, two), List(two, three)) where e.g. List(one, two)
Suppose we have the two classes given below: public class baseclass implements interface {}

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.