jack
jack

Reputation:

URL Rewriting + ASP.NET MVC

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?

Upvotes: 0

Views: 151

Answers (5)

Martijn Laarman
Martijn Laarman

Reputation: 13536

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.

Upvotes: 0

Haacked
Haacked

Reputation: 59041

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

Upvotes: 0

gius
gius

Reputation: 9437

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)
{
 ...
}

Upvotes: 2

Technowise
Technowise

Reputation: 1357

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

Upvotes: 0

Palantir
Palantir

Reputation: 24182

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

Upvotes: 1

Related Questions