0

I would like to route with

api/prefix/{controller}/{id}/{param}

as opposed to

api/prefix?Controller={controller}&Id={id}&Param={param}

In other words, I would prefer a URL without the name/value parameters.

The controller is defined like this:

/// <summary>
/// Employee API
/// </summary>
[System.Web.Mvc.RoutePrefix("api/employee")]
public class EmployeeController : ApiController
{
    ///<summary>
    /// Returns a collection of employees from a supplied CSV of EmployeeIds
    /// </summary>
    /// <paramref name="EmployeeIds">CSV of EmployeeIds. Pass "ALL" to include all employees. Defaults to "ALL".</paramref>
    /// <paramref name="PageSize">Number of rows to return. Defaults to all rows</paramref>
    /// <paramref name="PageNumber">Number of the page to return. Defaults to Page 0</paramref>
    /// <returns>IEnumerable<Employee></returns>
    [System.Web.Mvc.Route("id/{EmployeeIDs:alpha?}/{PageSize:int?}/PageNum:int?}")]
    public IHttpActionResult getEmployeesByIds(string EmployeeIds = "ALL",int PageSize = 0,int PageNumber = 0)
    {
        EmployeeDb db = new EmployeeDb();
        IEnumerable<Employee> e = db.getEmployeesById(EmployeeIds, PageSize, PageNumber);
        return Ok(e);
    }
}

How can I control the way the format of the URL?

4
  • Unclear what you're asking. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. Commented Mar 1, 2017 at 15:05
  • Why? controller/action/ is much cleaner than controller=mycontroller&action=myAction..... Commented Mar 1, 2017 at 15:20
  • @Ric exactly, I want the former and am getting the latter. Commented Mar 1, 2017 at 16:13
  • You should use attribute routing from web api learn.microsoft.com/en-us/aspnet/web-api/overview/… Commented Mar 8, 2017 at 6:29

2 Answers 2

1

You are using the System.Web.Mvc.RoutePrefix and the System.Web.Mvc.Route attributes, but you inherit from ApiController, so this is a Web API Controller, as opposed to an MVC Controller. Can you try switching over to System.Web.Http.RoutePrefix and System.Web.Http.Route?

Sign up to request clarification or add additional context in comments.

2 Comments

Good catch, thanks! I made this change and am now getting an error message that a route template can't start with a "/" or "~". None of my templates start with anything non-alpha, so trying to track it down.
You have typos in the URL. The parameter names must match exactly the templateparts in the URL, and also there is a missing { before PageNum. Try this [Route("id/{EmployeeIds:alpha?}/{PageSize:int?}/{PageNumber:int?}")]
1

The System.Web.Http one is for Web API; the System.Web.Mvc one is for previous MVC versions. MVC is web applications, Web API is HTTP services.

RoutePrefix should be inherited from System.Web.Http.

As ApiController is from System.Web.Http, it is not able to resolve your route.

This ms-doc has good info about attribute routing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.