Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to use Attribute Routing and having a difficult time getting different parameters to work. When I search by Name (second routine below) I get the error: "IIS 8.0 Detailed Error - 404.0 - Not Found". When I search by ID it works fine after I added the ":int" to the Route attribute.

How can I implement a Search by Name which is a string field in this scenario? I need to continue to use Attribute Routing.

Here is the Controller Code:

[Route("api/section/{id:int}")]
[HttpGet]
public string Get(int id)
{
    return "Searching By ID"
}

[Route("api/section/{sectionname}")]
[HttpGet]
public string GetByName(string sectionName)
{
    return "Search By Name";
}

Here is the WebApiConfig.cs code:

public static void Register(HttpConfiguration config)
{
    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );
}
share|improve this question
    
This code actually worked, but because the section name in the database has a "." embedded in the name, for some reason Web API doesn't like it. If I search with a name that does not have a "." in it, this will work fine. How do I specify a period in the call to this API that doesn't cause an error? My call looks like: localhost:55328/api/Section/Foundation.js –  user2975847 Apr 7 '14 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.