0

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?

5 Answers 5

2

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)
{
 ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

I have done this and it seems to now work. hehe will try and trace to see if it even hits the route!
Try to change the name Post somehing else - it might collide with the Post action (GET/POST) - just guessing :-)
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.
ok it actually seems that the post title is being used as the action
|
1

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 = "" }
);

5 Comments

if I do this 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?
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: haacked.com/archive/2008/03/13/url-routing-debugger.aspx.
0

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

1 Comment

Strictly speaking may be correct, but ASP.net MVC will work on IIS6 with some very simple steps, including URL rewriting. See here: blog.codeville.net/2008/07/04/…
0

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" }
);

Comments

0

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

This MSDN article goes in to great detail how to set up default values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.