2

In my ASP.Net MVC 4 project, I have a controllers in a sub-folder in controller folder-

/Controllers
    /GroupA
        /AbcController.cs

In AbcController, i have two methods-

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Edit(string value)
    {
        ViewBag.Message = value;
        return View();
    }

RouteConfig.cs -

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

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

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

but when i browse http://localhost:2240/groupa/abc/edit/somevalue

, 'somevalue' is not passed to the method. It shows null.

What i am missing here?

3
  • 1
    if I recall correctly, isn't it solved by either .../groupa/abc/edit?somevalue as url or changing public ActionResult Edit(string value) to public ActionResult Edit(string id) i.e. you name your parameter id in the route, but value in the method Commented Oct 31, 2013 at 19:01
  • 1
    MVC Areas will give you another hierarchical level for your routes ("subfolders"). Commented Oct 31, 2013 at 19:01
  • @Jasen, Yes MVC Area is the way to go. Before I also used the way OP is doing, but only recently I learnt about Areas and it is very easy to use. Commented Aug 23, 2015 at 7:51

1 Answer 1

6

In your route, your parameter is declared as id while in your action method it's declared as value. Pick one and stick to it.

routes.MapRoute(
    name: "TestRoute",
    url: "GroupA/{controller}/{action}/{value}",
    defaults: new { controller = "AbcController", action = "Index", value = UrlParameter.Optional }               
);

public ActionResult Edit(string value)
{
    ViewBag.Message = value;
    return View();
}

Edit : while we're on the subject, I recommend you give AttributeRouting a look.

Sign up to request clarification or add additional context in comments.

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.