0

I have an ApiController, quite simple, like this:

public class AssetController : ApiController
{
// removed for brevity
}

When I insert a route to it from a view, the url created is something like:

http://host/Asset

but I would like to customize the name, so that it becomes this:

http://host/assets

How can I specify a custom name for my controller, without resorting to a complete custom routing table?

Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
  • For making it lowercase... have you checked out this answer http://stackoverflow.com/questions/878578/how-can-i-have-lowercase-routes-in-asp-net-mvc and http://stackoverflow.com/questions/6168270/making-url-lowercase-any-easy-or-builtin-way ? They tend to point to a CodePlex project here http://lowercaseroutesmvc.codeplex.com/ check out section: (Step 4. (if using ASP.NET Web API) - Remap Your HTTP) – Mark Jones Aug 23 '12 at 10:34
  • Thanks, I've been looking at that, and I've been going through the source code but I can't find anything that helps me. I think the only way forward today is a custom routetable. – Dave Van den Eynde Aug 23 '12 at 17:41
  • I've updated the question body to make it more clear. – Dave Van den Eynde Aug 23 '12 at 17:52

3 Answers3

1

When I insert a route to it from a view, the url created is something like: http://host/Asset

You haven't really shown how you are doing this inserting but the following should work fine:

@Url.RouteUrl("DefaultApi", new { httproute = "false", controller = "assets" })

and if you want an absolute url you could specify the protocol scheme as third argument:

@Url.RouteUrl("DefaultApi", new { httproute = "false", controller = "assets" }, "http")

And in order to obey RESTFul conventions you should rename your controller to AssetsController.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Someone helpful, thank you. It does make me rename my controller to influence the name. I'm trying to get the RESTFul convention of having a plural name, but I don't see how that's supposed to be the controller's concern. – Dave Van den Eynde Aug 23 '12 at 18:10
  • It's the controller's concern in terms of conventions. That's how ASP.NET MVC routing works. So follow those conventions. – Darin Dimitrov Aug 23 '12 at 22:58
1

I'd recommend looking at the https://github.com/mccalltd/AttributeRouting library. It handles this aspect quite well by putting an attribute right on each function and giving it a specific route (which can be anything).

Chris
  • 772
  • 3
  • 8
0

I've had to resolve this issue so I've opted to adjust my routing table to reflect the API that I really want.

Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90