1

I have the following in my HomeController:

public ActionResult Share(string id)
{
    Debate debate;
    try
    {
        debate = Db.Debates.Single(deb => deb.ShareText.Equals(id));
    }
    catch (Exception)
    {
        return RedirectToAction("Index");
    }
    return View(debate);
}

This allows me to call the url this way:

http://localhost:50499/Home/Share/SomeTextHere

I configured the route this way in the routeconfig file:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Share",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Share", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

I want to be able how to call the URL this way:

http://localhost:50499/Debate/SomeTextHere

I have been struggling with this with no luck. What is the best way to achieve it?

5
  • 2
    You cannot remove both the controller and action name from the url. You routes need to be distinguishable in some way. You could create a route for say .../Share/SomeTextHere Commented Sep 26, 2016 at 1:02
  • 2
    Note also that both the routes you have shown are identical - they both match any url that contains 2 segments with an optional 3rd segment. Your 2nd route can never be hit (you may as well delete the 1st) Commented Sep 26, 2016 at 1:03
  • @StephenMuecke I changed the URL to localhost:50499/Debate/SomeTextHere Is this possible? Commented Sep 26, 2016 at 1:09
  • @MRFerocius you can set that as the route and set the defaults to the controller and action you want. Where do you want that url to map to? Commented Sep 26, 2016 at 1:09
  • If you want ../Debate/SomeTextHere then change the first route definition to url: "Debate/{id}", Commented Sep 26, 2016 at 1:15

1 Answer 1

2

As mentioned in the comments by @StephenMuecke you would need to change the route to not make it so general.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Share",
        url: "Debate/{id}",
        defaults: new { controller = "Home", action = "Share", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

the Share route would now allow http://localhost:50499/Debate/SomeTextHere to route to the appropriate controller action.

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

2 Comments

I think OP wants just ../Debate/xx so its url: "Debate/{id}",
@StephenMuecke you are correct. I misread the requirement. Thanks

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.