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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:38:21+00:00 2026-05-29T10:38:21+00:00

I have a controller [MyAttribute] public class MyController: Controller { public MyController(InterfaceA a, InterfaceB

  • 0

I have a controller

[MyAttribute]
public class MyController: Controller
{
    public MyController(InterfaceA a, InterfaceB b)
    {
    }
}

I wanted InterfaceA always to bind to ClassA if the request is coming from a controller with the MyAttribute. So I did a call.

Bind<InterfaceA>.To<ClassA>().WhenClassHas<MyAttribute>();

One of the instantiations of InterfaceB, ClassB, has a constructor like this.

public ClassB(InterfaceC c)

I want InterfaceC to bind to Class C when the called controller had the MyAttribute. However, my previous binding won’t work, because the parent class is an intermediary class, not the controller itself. Is there anyway to write the binding so it will work from anywhere provided the calling controller had the attribute?

Edit: My solution using Remo’s advice

public static IBindingInNamedWithOrOnSyntax<TBinding> WhenControllerHasAttribute<TBinding>(this IBindingWhenSyntax<TBinding> instance, Type type)
{
    Func<IRequest, bool> hasAttribute = (request) =>
    {
        while (request.ParentRequest.Target != null)
        {
            request = request.ParentRequest;
        }
        return request.Target.Member.ReflectedType.IsDefined(type, false);
    };
    return instance.When(hasAttribute);
}
  • 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-29T10:38:22+00:00Added an answer on May 29, 2026 at 10:38 am

    Yes you can you must use When(Func<IRequest, bool> condition) instead and write some custom code.

    WhenClassHas uses the following Func:

    request => request.Target.Member.ReflectedType.HasAttribute(attributeType)
    

    You have to extend this and iterate up in the injection context using request.ParentRequest

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

Sidebar

Related Questions

I have a controller with an action method as follows: public class InventoryController :
I have a controller called MetricsController with a single action method: public class MetricsController
I have controller and it has following code: class Company_ModuleName_NameController extends Mage_Core_Controller_Front_Action { public
When submitting one form from view, how can i also read or have controller
I have multiple controller actions that takes an id public ActionResult Get(int? id) {
I have a controller defined as: [AcceptVerbs(HttpVerbs.Post)] public JsonResult PostMoreData(DataContracts.Address address, DataContracts.GeoLocation geoLocation) {
I have a controller that all my controllers inherit from and I need to
I have controller: package plugin class TestController { def simply = {[name:new Date()]} }
I have controller: class AccountController < ApplicationController def index end private def current_account @current_account
Here is what I have: Controller [HttpPost] public ActionResult GetNumbers(int id) { List<int> privIDs

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.