Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Web API and route mapping

$
0
0

How do I got about allowing my application to accept more that just ID in controller functions? Here's an example. If I write this function

        public IHttpActionResult GetBadge(string id)
        {
            var badge = GetAllBadges().FirstOrDefault(b => b.slug == id);
            if (badge == null)
            {
                return NotFound();
            }
            return Ok(badge);
        }

It works just great browsing to myapplication/api/badge/badge-name

However, if I change that to 

public IHttpActionResult GetBadge(string slug)
{
var badge = GetAllBadges().FirstOrDefault(b => b.slug == slug);
if (badge == null)
{
return NotFound();
}
return Ok(badge);
}

When I browse to the same page it returns all the data. I put a break point on that function, and it never even gets hit. How can I make it so I don't always have to name that argument id? I started this as a web forms empty project, and then checked web API, if that helps any. 


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>