3

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
  • setting the routing rules worked and was the easiest. I was hoping for something that I could put in the controller class so that the setting would be close to the controller and not located in a different place where another developer might not notice it. Also this leaves the old route /ContactUs/ still working. might see if I can get the iis7 url rewriter to do a perm redirect. Thanks a ton for the help!
    – Zack
    Commented Apr 23, 2009 at 16:46
  • Happy to help. Welcome to the site!
    – Peter J
    Commented Apr 23, 2009 at 16:47

4 Answers 4

9

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" }
);
2
  • Looks good. This (and Richard's answer) is probably a good solution. Commented Apr 23, 2009 at 16:03
  • 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
    Commented Apr 23, 2009 at 16:20
2

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.

1

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.

1
  • your second route is not required if you append /{action} to the path of the first. Commented Apr 23, 2009 at 15:56
0

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.

2
  • I am not sure if that article is related to what he's asking. Commented Apr 23, 2009 at 15:57
  • Cheers - you can tell it's the end of the day, I'm not reading things properly! Commented Apr 23, 2009 at 16:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.