views:

67

answers:

5

Hi Guys

I have a issue that they users here want urls like http://host/Post/PostTitle

Is this possible?

As your not passing in the action?

regards

+1  A: 

Sure, you just make an appropriate route. It depends very much on other routes you have in your map, but this shoult work in almost any situation. Put it before the default route, though.

routes.MapRoute(
    "Login",
    "Page/{id}",
    new { controller = "Page", action = "index", id = "" }
);
Palantir
if I do this http://localhost:3630/Post/Neque_porro_quisquam_est routes.MapRoute( "Post", // Route name "Post/{title}", // URL with parameters new { controller = "Post", action = "Index", title = "" } // Parameter defaults );I get resource cannot be found! :(
Do you have the Post controller in place and the Index action taking a string (not an int) as input? Did you recompile everything?
Palantir
@jack What other MVC pages? Do they work?
gius
yup :( I have done that also restarted the internal webserver
Is there a way you can trace this?
Yes, there is a way to debug routes - check this: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx.
gius
A: 

You can infer the action by setting up appropriate URL Routing schemes

This msdn atricle goes in to great deatil how to set up default values.

Martijn Laarman
A: 

AFAIK, URL Rewriting feature is only introduced in IIS 7. Read this blog for more details on that.

Technowise
Strictly speaking may be correct, but ASP.net MVC will work on IIS6 with some very simple steps, including URL rewriting. See here: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
Palantir
+2  A: 

Palantir is right, you can make a route like this:

routes.MapRoute(
    "Posts", // route name
    "Post/{PostTitle}",
    new { controller = "Post", action = "Index" }
);

And then , in your PostController, you should create action as follows:

public ActionResult Index(string PostTitle)
{
 ...
}
gius
I have done this and it seems to now work. hehe will try and trace to see if it even hits the route!
I meant it seems to not work hehe
Try to change the name Post somehing else - it might collide with the Post action (GET/POST) - just guessing :-)
gius
Heh I tried that already thought it might have been but its not working :(
Weird...it's going to be a stupid typo or something :-( - try to use the Route Debugger. I would suggest to create another action for the controller so that you know if the call gets to the controller (and fails on the action) or does not even get to the controller.
gius
ok it actually seems that the post title is being used as the action
ok figured it out I had the route below the default route :( *feels stupid*
:-) (sh)it happenes. Happy you finally solved it...
gius
A: 

Try changing your PostController to this (for testing purposes).

public class PostController : Controller
{
  public string Index(string postTitle)
  {
    return postTitle;
  }
}

And your route defined as

routes.MapRoute(
    "Posts", // route name
    "Post/{PostTitle}",
    new { controller = "Post", action = "Index" }
);
Haacked