0
votes

I want to have two routes, for example:

http://localhost:1227/Product

and

http://localhost:1227/Category/

trailing slash is the only difference.

In first case I want to show information about product in other case about category. "Product" and "Category" are names of some products or categories

Is it possible to implement?

My approach:

Routes:

routes.MapRoute("category route", "{name2}", new { controller = "test", action = "GoToView2" }, new { name2 = ".*/?" });
routes.MapRoute("product route", "{name}", new { controller = "test", action = "GoToView1"});

Controller:

public class TestController : Controller
    {
        //
        // GET: /Test/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GoToView1(string name)
        {
            // call to db to get some information about product
            return View((object) name);
        }

        public ActionResult GoToView2(string name2)
        {
            // call to db to get some information about category
            return View((object)name2);
        }
    }

Index View:

......

<br/>
<a href="@Url.Action("GoToView1", new { name = "Prouct"} )">Show proudct</a>
<br/>
<a href="@Url.Action("GoToView2", new { name2 = "Category" + "/"  } )">Show category</a>

......

But I see that only "GoToView2" is called for two link.

Any ideas how to fix it?

Thanks,

1

1 Answers

0
votes

Why would one wants to do that? in your case you have to completely different controller, namely Product and Category and two different action, and even if we could implement the same URL to call different action based on slash, it would be a code smell(bad design), in your case you can simply call TestController/GoToView1 and TestController/GoToView2, if this is not the case, tell us exactly what problem you are trying to solve, maybe there would be a better approach.