Reputation: 205
I'm looking for a way to turn variables in a URL after the question mark into a simple notation with a slash.
For example: I would like to make it possible to enter this link:
http://localhost:50830/Controller/View?Name=Test
in this form into the browser
http://localhost:50830/Controller/View/Test
The controller then should recognize "Test" as the Name variable. So basically the two links should give the same result.
Is this possible?
Upvotes: 2
Views: 3288
Reputation: 76258
You need to define a route for that.
For example:
routes.MapRoute(
"Test", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Controller", action = "View", Name = "" } // Parameter defaults
);
Note the Name
parameter. By default MVC routes are setup to look for Id
parameter.
Corresponding Action:
public ActionResult View(string name)
{
...
}
And if you're using MVC 5, you can define the routes using Attributes as well:
[RoutePrefix("Home")]
public class HomeController : Controller
{
[Route("view/{name}")]
public ActionResult View(string name) { ... }
}
Upvotes: 2