views:

236

answers:

4

I was recently asked to modify a small asp.net mvc application such that the controler name in the urls contained dashes. For example, where I created a controller named ContactUs with a View named Index and Sent the urls would be http://example.com/ContactUs and http://example.com/ContactUs/Sent. The person who asked me to make the change wants the urls to be http://example/contact-us and http://example.com/contact-us/sent.

I don't believe that I can change the name of the controller because a '-' would be an illegal character in a class name.

I was looking for an attribute that I could apply to the controller class that would let me specify the string the controller would use int the url, but I haven't found one yet.

How can I accomplish this?

+2  A: 

I don't believe you can change the display name of a controller. In the beta, the controller was created using route data "controller" with a "Controller" suffix. This may have changed in RC/RTM, but I'm not sure.

If you create a custom route of "contact-us/{action}" and specify a default value: new { controller = "ContactUs" } you should get the result you are after.

Richard Szalay
+7  A: 

Simply change the URL used in the route itself to point to the existing controller. In your Global.asax:

routes.MapRoute(
  "Contact Us",
  "contact-us/{action}/",
  new { controller = "ContactUs", action = "Default" }
);
Peter J
Looks good. This (and Richard's answer) is probably a good solution.
Kezzer
Not precisely the "way" Zack envisioned the solution, but a more valid one, I believe. This way, there's no need to bend controller names for the sole purpose of friendly URLs.
Peter J
+1  A: 

You need to configure routing. In your Global.asax, do the following:

public static void RegisterRoutes(RouteCollection routes)
{
  ...
  routes.MapRoute(
    "route-name", "contact-us/{action}", // specify a propriate route name...
    new { controller = "ContactUs", action = "Index" }
  );
  ...

As noted by Richard Szalay, the sent action does not need to be specified. If the url misses the .../sent part, it will default to the Index action.

Note that the order of the routes matter when you add routes to the RouteCollection. The first matched route will be selected, and the rest will be ignored.

knut
your second route is not required if you append /{action} to the path of the first.
Richard Szalay
A: 

One of the ASP.NET MVC developers covers what Iconic is talking about. This was something I was looking at today in fact over at haacked. It's worth checking out for custom routes in your MVC architecture.

EDIT: Ah I see, you could use custom routes but that's probably not the best solution in this case. Unless there's a way of dealing with the {controller} before mapping it? If that were possible then you could replace all "-" characters.

Kezzer
I am not sure if that article is related to what he's asking.
çağdaş
Cheers - you can tell it's the end of the day, I'm not reading things properly!
Kezzer