2

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
    });
14
  • You route definitions need to match the parameter names - "TestRoutes/Display/{s}/{i}" (and only the last parameter can be UrlParameter.Optional) Commented Apr 15, 2016 at 5:56
  • I am unable to code it properly, Can I use attributes instead of Maproute thing? Commented Apr 15, 2016 at 6:18
  • What is not working? It just needs to be routes.MapRoute(name: "Professional", url: "TestRoutes/Display/{s}/{i}", new { controller = "TestRoutes", action = "Display" });. But in your Index() method it needs to be return RedirectToAction("Display", new { s = "hi", i = 2 }); (not f) Commented Apr 15, 2016 at 6:21
  • I am so sorry, I am stretching it a lot but again it's giving me an error " Named argument specifications must appear after all fixed arguments have been specified" Am I missing basics of routing ? Commented Apr 15, 2016 at 6:34
  • Sorry, I left of the defauts: in front of new { .... Commented Apr 15, 2016 at 6:35

5 Answers 5

4

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
}
Sign up to request clarification or add additional context in comments.

9 Comments

Default before new :)
Lazy cut and paste :)
I didn't do it intentionally, I guess it was a click by mistake, I didn't even realize when did I click. So sorry.
Hey Stephen, I'd like to ask questions which are not related to this thread but others which you've put on hold, I hope you won't mind.
@Nil, Sorry, I don't understand your comment. What questions are your referring to?
|
0

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
}

2 Comments

I may require id property for further operations. But even if I remove it, I am not able to see any changes in the url. If possible to you can you elaborate a little bit?
Then add one more attribute in route url for example routes.MapRoute("Professional", "{controller}/{action}/{name}/{id}", new {controller = "TestRoutes", action = "Display", id = UrlParameter.Optional}, and action like public ActionResult Display(string name, int? id){ // action goes here}
0

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(); }

Comments

0

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/

3 Comments

Thanks for the link but I am not getting your solution. Do you mean I should write diff controller for routing?
No, RouteConfig.cs is under "App_Start" folder in your MVC project. If you want to use Attribute Routing, you have to change your RouteConfig.cs content and your other controllers like in my post. I think Attribute Routing is a more flexible solution than Convention Routing. And you can easily control your routes.
Attribute routing isn't working for me ( I am unable to use it, I meant) I get default url with id? etc
0

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 }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.