user2094139
user2094139

Reputation: 327

MVC Reroute to accept Key

working on an MVC project and I'm having a tough time with rerouting my URL. What I currently have is

http://dev.mywebsite.com/s/index?Key=abc123

which then runs the index action and completes as I'd like it to I'd like to be able to type in

http://dev.mywebsite.com/s/abc123

and run the index action like normally

I currently have

        routes.MapRoute(null, "s/index/{id}", new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        }
             );

but I'm kind of stuck as to where to go from here. Any assistance would be greatly appreciated. Thanks

Edit: My full routeconfig class

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("sites/{*pathInfo}");

        routes.MapRoute(null, "s/index/{key}", new
        {
            controller = "S",
            action = "Index",
            key = UrlParameter.Optional
        }
        );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Site", action = "Index", id = UrlParameter.Optional },
            new[] { "Custom.Web.Controllers" }
        );
    }

in my controller I have the actionresult index as

    public ActionResult Index(string Key)
    {
        return Redirect(workflow.RetrieveURL(Key));
    }

Upvotes: 1

Views: 110

Answers (1)

Andre Calil
Andre Calil

Reputation: 7692

So, after all our comments, the solution is:

routes.MapRoute(null, "s/{Key}",
new {
  controller = "Home",
  action = "Index",
  Key = UrlParameter.Optional
});

Place this rule before all the others to give it preference

Upvotes: 1

Related Questions