Nil
Nil

Reputation: 176

How to display only name in url instead of id in mvc.net using routing

I am getting url like http://localhost:49671/TestRoutes/Display?f=hi&i=2

I want it like http://localhost:49671/TestRoutes/Display/hi

I call it from Index method.

[HttpPost]
public ActionResult Index(int? e )
{
    //  return View("Display", new { f = "hi", i = 2 });
    return RedirectToAction("Display", new { f = "hi", i = 2 });
}

Index view

@model Try.Models.TestRoutes
@using (Html.BeginForm())
{
    Model.e = 5 ; 
    <input type="submit" value="Create" class="btn btn-default" />
}

Display Action method

// [Route("TestRoutes/{s}")]
public ActionResult Display(string s, int i)
{
    return View(); 
}

Route config file

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Professional", // Route name
        "{controller}/{action}/{id}/{name}", // URL with parameters
         new { controller = "TestRoutes", action = "Display", s = UrlParameter.Optional, i = UrlParameter.Optional
    });
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id =   UrlParameter.Optional
    });

Upvotes: 2

Views: 5687

Answers (5)

Ahmad Ghasemi
Ahmad Ghasemi

Reputation: 7

Another way to solve this problem is to put the proper route before the default route, as follows:

routes.MapRoute(name: "MyRouteName", url: "Id", defaults: new { controller= "Home", action = "Index",id= Id });

Default route:

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

Upvotes: 0

user3559349
user3559349

Reputation:

You need to change your route definition to

routes.MapRoute(
    name: "Professional",
    url: "TestRoutes/Display/{s}/{i}",
    default: new { controller = "TestRoutes", action = "Display", i = UrlParameter.Optional }
);

so that the names of the placeholders match the names of the parameters in your method. Note also that only the last parameter can be marked as UrlParameter.Optional (otherwise the RoutingEngine cannot match up the segments and the values will be added as query string parameters, not route values)

Then you need to change the controller method to match the route/method parameters

[HttpPost]
public ActionResult Index(int? e )
{
    return RedirectToAction("Display", new { s = "hi", i = 2 }); // s not f
}

Upvotes: 4

kkakkurt
kkakkurt

Reputation: 2800

You can also try using Attribute Routing. You can control your routes easier with attribute routing.

Firstly change your RouteConfig.cs like that:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}/{id}",
        //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        //);
    }
}

After that change your controller files like that:

namespace YourProjectName.Controllers
{
    [RoutePrefix("Home")]
    [Route("{action}/{id=0}")]
    public class HomeController : Controller
    {
        [Route("Index")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("ChangeAddress/{addressID}")]
        public ActionResult ChangeAddress(int addressID)
        {
            //your codes to change address
        }
}

You can also learn more about Attribute Routing in this post: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

Upvotes: 0

Karthik M R
Karthik M R

Reputation: 990

Remove the maproute code: routes.MapRoute( "Professional", // Route name "{controller}/{action}/{id}/{name}", // URL with parameters new { controller = "TestRoutes", action = "Display", s = UrlParameter.Optional, i = UrlParameter.Optional }); Use attribute routing code: [Route("TestRoutes/{s}/{i?}")] public ActionResult Display(string s, int? i) { return View(); }

Upvotes: 0

user3275644
user3275644

Reputation: 11

change your route as routes.MapRoute( "Professional", // Route name "{controller}/{action}/{name}", // URL with parameters new { controller = "TestRoutes", action = "Display" } // Parameter defaults );

and your action as

public ActionResult Display(string name)
{
     //action goes here
}

Upvotes: 0

Related Questions