In my MVC web app I am to route all urls with exactly one url segment to the same controller action. Like for example:
http://example.com/onePage
Here's my action method in the controller:
public ActionResult SomeAction(string urlSegment)
{
...
}
Now, I want the url segment (like "onePage" from the example), to be sent as input to the action method.
Can you show what the MapRoute should look like to make this happen?
答案 0 :(得分:0)
You should be able to do that by editing your RouteConfig.cs file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "Home|Account" }
);
routes.MapRoute(
"CustomRoute",
"{*urlSegment}",
new { controller = "MyController", action = "SomeAction" }
);
}
Change MyController
to whatever the name of your controller is called.
If you want to match exactly one segment, remove the *
.
答案 1 :(得分:0)
It would probably look something like this.
routes.MapRoute(
"UrlSegment", // Route name
"{urlSegment}", // URL with parameters
new { action="SomeAction",controller="ControllerName" }, // parameter defaults
new[] { "Namespace.Controllers" } // controller namespaces);
I'm on my phone so unfortunately can't verify but that should get what you're looking for.