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?

3
  • For making it lowercase... have you checked out this answer stackoverflow.com/questions/878578/… and stackoverflow.com/questions/6168270/… ? They tend to point to a CodePlex project here lowercaseroutesmvc.codeplex.com check out section: (Step 4. (if using ASP.NET Web API) - Remap Your HTTP) Commented Aug 23, 2012 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. Commented Aug 23, 2012 at 17:41
  • I've updated the question body to make it more clear. Commented Aug 23, 2012 at 17:52

3 Answers 3

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.

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

2 Comments

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.
It's the controller's concern in terms of conventions. That's how ASP.NET MVC routing works. So follow those conventions.
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).

Comments

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.

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.