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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:10:01+00:00 2026-06-17T05:10:01+00:00

I swear this doesn’t make sense. Given I have this HttpRouteCollection from Web API,

  • 0

I swear this doesn’t make sense.

Given I have this HttpRouteCollection from Web API, I am filtering based on some custom route types, specifically IAttributeRoute. I use httpRoutes.OfType<IAttributeRoute>() but the loop is acting like there’s no elements.

To be clear, when this loop gets hit, the entire collection are of type HttpAttributeRoute which directly implement IAttributeRoute. Furthermore, I have the same loop operating on the regular RouteCollection.

This doesn’t work:

foreach (IAttributeRoute route in GlobalConfiguration.Configuration.Routes.OfType<IAttributeRoute>()) {
    if (!string.IsNullOrWhiteSpace(route.RouteName) && !json.ContainsKey(route.RouteName)) {
        json.Add(route.RouteName, "/" + route.Url);
    }
}

Yet, this works fine:

foreach (var route in GlobalConfiguration.Configuration.Routes)
{
    if (route is IAttributeRoute) // uhhhh?
    {
        var r = route as IAttributeRoute;
        if (!string.IsNullOrWhiteSpace(r.RouteName) && !json.ContainsKey(r.RouteName))
        {
            json.Add(r.RouteName, "/" + r.Url);
        }
    }
}

I swear I am not lying, it only works in the latter code. Like I said, doing this on a normal route collection is fine. The normal route collection has other types, not just IAttributeRoute, but the HttpRouteCollection only has IAttributeRoute, in its current state (in the future it could have different type routes). Could that be a factor?

Am I missing something or isn’t OfType doing what I’m doing internally?

  • 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-17T05:10:02+00:00Added an answer on June 17, 2026 at 5:10 am

    I would guess it’s an issue with the way HostedHttpRouteCollection handled references to its internal collection. The GetEnumerator only returns routes of type HttpWebRoute already:

    public override IEnumerator<IHttpRoute> GetEnumerator()
    {
        return (
            from httpWebRoute in this._routeCollection.OfType<HttpWebRoute>()
            select httpWebRoute.HttpRoute).GetEnumerator();
    }
    

    So, while it is initialized with all of the routes from RouteTable.Routes, it doesn’t return them all as IEnumerable.

    This being the case, I wonder when you say “it works” in the second example, does this mean it actually iterates all the items you would expect? Looking at the implementation and running an equivalent to what you posted, I would not think that it does.

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

Sidebar

Related Questions

I could swear this was working the other day: var resultSet = (from o
I swear, I have this exact thing working on another page. I'm such a
Some days I swear I'm going mad. This is one of those days. I
I swear I'm going to learn more JavaScript... I have this page (which really
(A jQuery Question about filtering stuff on StackOverflow. I swear, it doesn't belong on
I swear this script worked fine last night but can't get it to work
I swear this used to work, but it's not in this case. I'm trying
I swear this assignment will be the end of me, I've been researching this
I've done my homework - I swear, I've been researching this for a bit.
I may be completely off on this, but I swear I remember reading somewhere

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.