0

I have a url which says www.test.com/12345. When the user hit this url, he should be redirected to respective action method where "12345" accepts as its parameter.

I have a slight idea that can be used with RouteConfig but still no clear picture.

Can any one help me on this please?

Action method is

[HttpPost]
public ActionResult DetailsByCode(string code)
{    
     IEnumerable<IProductListDto> prdctListDto = _productListDetails.GetProductListByCode(accessCode);
     Return "success";
}

and routeconfig is

routes.MapRoute(
     name: "Accesscode",
     url: "{Areas}/{controller}/{action}/{id}",
     defaults: new { Areas = "Student", controller = "Student", action = "DetailsByCode", id = string.Empty }
 ); 

My complete routeconfig.cs is

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

        routes.MapRoute(
            name: "DetailsByCodeRoute",
            url: "{Areas}/{controller}/{action}/{id}",
            defaults: new { Areas = "Student", controller = "Student", action = "DetailsByCode" }
          );

        routes.MapRoute(
            name: "Accesscode",
            url: "{Areas}/{controller}/{action}/{id}",
            defaults: new { Areas = "Student", controller = "Student", action = "DetailsByCode", id = string.Empty }
        );

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

        routes.MapRoute(
            name: "ChangePassword",
            url: "{Areas}/{controller}/{action}/{id}",
            defaults: new { Areas = "User", controller = "User", action = "ChangePassword", id = string.Empty }
        );
        routes.MapRoute(
           name: "PasswordReset",
           url: "{Areas}/{controller}/{action}/{id}",
           defaults: new { Areas = "User", controller = "User", action = "PasswordReset", id = string.Empty }
       );
TechNo
  • 615
  • 2
  • 7
  • 24
  • 1
    So what happens when you type that url now? Is it giving any exception? Post your `routeconfig.cs` and also the `action` method code. – ramiramilu Jan 18 '16 at 06:33
  • You will need to specify the controller and method or define a specific route that includes at least some prefix to identify the controller/action. –  Jan 18 '16 at 06:38
  • @ramiramilu i updated my question with Action method and Route config – TechNo Jan 18 '16 at 06:42
  • So are you trying to hit `DetailsByCode` when you type `www.test.com/12345`? What is your controller name? – ramiramilu Jan 18 '16 at 06:46
  • @ramiramilu yes and my controller name is Student – TechNo Jan 18 '16 at 06:53
  • Then you should have one more `route` on top of all other routes. `routes.MapRoute( name: "DetailsByCodeRoute", url: "{code}", defaults: new { Areas = "Student", controller = "Student", action = "DetailsByCode"} );` – ramiramilu Jan 18 '16 at 06:54

2 Answers2

0

In your situation you can use Route Constraints.

For example read this post. http://www.c-sharpcorner.com/UploadFile/ff2f08/custom-route-constraints-in-Asp-Net-mvc-5/

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
0

Instead of adding new route i added one line in Routeconfig.cs which is for Enable Attribute routing.

routes.MapMvcAttributeRoutes();

and one line before the action method

[Route("{accesscode:int}")]
TechNo
  • 615
  • 2
  • 7
  • 24