1
votes

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));
    }
1
Could you please try the pattern "s/{key}", also changing id = UrlParameter.Optional, to key = UrlParameter.Optional?Andre Calil
@AndreCalil unfortunately I still get a server error, I'd also like to add that I'd like s/MyOtherAction to run as normal, but I just want when I go to dev.mywebsite.com/s/abc123 to run the index action with abc123 as the keyuser2094139
Could you provide the error details? Also, it would be nice to have the signature of Home/Index action. Do you have any other routing rule?Andre Calil
@AndreCalil I have added extra code as requested. Please take a look and let me know what you think. I keep getting the 404 error. 'page not found'user2094139
Yes, and your pattern should be "s/{key}" instead of "s/index/{key}"Andre Calil

1 Answers

1
votes

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