MvcRouteTester icon indicating copy to clipboard operation
MvcRouteTester copied to clipboard

"Global" controller routes not respected

Open justmara opened this issue 10 years ago • 0 comments

Since MVC/WebAPI 5.2 there is an option to add single global route attribute to controller so all the actions in it will match:

    [RoutePrefix("files"), Route("{container}/{id}")]
    public class FilesController : ApiController
    {
        [HttpGet]
        public async Task<IHttpActionResult> GetDescription(string container, string id)
        {
            return Ok();
        }

        [HttpDelete]
        public async Task<IHttpActionResult> Delete(string container, string id)
        {
            return Ok();
        }
    }

This notation actually works flawlessly and all requests land as expected. But test fails:

    [TestMethod]
    public void GetDescription()
    {
        var expectations = new
        {
            controller = "Files",
            action = "GetDescription",
            container = "someContainer",
            id = "someFile"
        };
        RouteAssert.HasApiRoute(_host.Config, "/files/someContainer/someFile", HttpMethod.Get, expectations);
    }

with error: Expected 'someFile', got missing value for 'id' at url '/files/someContainer/someFile'. Simply copying the route attribute from controller to method declaration makes it pass:

    [HttpGet, Route("{container}/{id}")]
    public async Task<IHttpActionResult> GetDescription(string container, string id)

justmara avatar Dec 23 '14 09:12 justmara