0

I'm trying to configure a route for the url pattern "http://localhost:1234/#/animal"

Basically I want different actions to be executed for "http://localhost:1234" and "http://localhost:1234/#/animal"

So I added a route like below.

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

But it still uses the Default route shown below and not DefaultHashed route. What am i missing?

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

Edit: This is an ASP.NET MVC with Angular SPA. The Index action of default controller is shown below.

 public ActionResult Index(int id)
        {
            if (User?.IsAuthenticated)
            {
               return View("/dist/Index.cshtml");
            }
            return RedirectToAction("SignIn", "Authentication");
        }

Here I want to redirect to external url if the posted url is "http://localhost:1234". if not I should load the angular page.

NewBie
  • 81
  • 1
  • 6
  • I don't think you will be able to do this in MVC, as url portion after # is trimmed and it is not posted to the server. This type is routing schemes are common in Single Page Application built with frameworks like Angular or React – Yogi Jun 19 '19 at 05:07
  • @Yogi thanks for the info. But I need to achieve this some how. I have edited question for more details. Just wondering if there is any alternative? – NewBie Jun 19 '19 at 05:29
  • Bascially **you can't**. The server never recieves the fragment identifier. – Erik Philips Jun 19 '19 at 05:30

0 Answers0