0

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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

If the request is to match exactly one urlSegment, then shouldn't that that * be removed?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.