0

I am in the process of learning MVC, and currently looking at routing.

I have the following issue: Here is my RegisterRoutes method:

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

    routes.MapRoute("Customer", "{controller}/{action}/{objCustomer}",
            new {controller = "Customer", action = "CreateCustomer", id = UrlParameter.Optional});
}

If I ran my application, should hxxp://localhost:12454/ not display the View called by CreateCustomer action in the CustomerController, in other words, the URL should like this? hxxp://localhost:12454/Customer/CreateCustomer

NOTE: I replaced http with hxxp, to not try and create a link

What am I not understanding correctly here?

Here is my whole Global.asax.cs class.

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

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

        routes.MapRoute("Customer", "{controller}/{action}",
              new { controller = "Customer", action = "CreateCustomer", UrlParameter.Optional});
    }
}

And here is my CustomerController:

public class CustomerController : Controller
{
    // GET: Customer
    public ActionResult ShowCustomer(Customer objCustomer)
    {
        return View(objCustomer);
    }

    public ActionResult CreateCustomer()
    {
        return View();
    }
}
8
  • just check that in your routes there is extra , UrlParameter.Optional Commented Sep 18, 2014 at 11:08
  • make sure that this route is above all the other routes Commented Sep 18, 2014 at 11:13
  • What do you mean this route should be above all other routes? I only have this one route defined. Commented Sep 18, 2014 at 11:15
  • I have a model Named Customer as well, within the Model folder. Will this cause any problems? Commented Sep 18, 2014 at 11:20
  • 1
    i don't think so..just try it..and i mean to say that remove this route from global.asax file and paste it on top above default route in routeconfig.cs file in appstart folder. Commented Sep 18, 2014 at 11:22

1 Answer 1

1

Instead of 'id' you are using objCustomer in your routes then you have to specify objCustomer as optional route parameter.

Modify routes as shown below :

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

  routes.MapRoute("Customer", "{controller}/{action}/{objCustomer}",
        new {controller = "Customer", action = "CreateCustomer", objCustomer = UrlParameter.Optional});
}

Make all the custom routes inside routeconfig.cs file inside AppStart folder and don't forget to put this custom route above the default route.

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

5 Comments

@akemp..make sure that you have a Customer controller inside controller folder with createcustomer action and for testing put a breakpoint on createcustomer action and see if its coming there or not
My CreateCustomer nethod does not except any parameters, I think this was the issue. But how do I change the MapRoute() to accomodate no parameter in the default url? For instance, I have the following now, but does not work: routes.MapRoute("Customer", "{controller}/{action}", new { controller = "Customer", action = "CreateCustomer" });
@akemp..the route you showed should work..see there must be problem somewhere else and as shown in my answer objCustomer is optional you can give some value to objCustomer or not it will not create any problem.
I do not see anything that can go wrong. I am gonna update my question now, and post my whole Global.asax.cs, and then you would be able to see
@akemp..also post controller action

Your Answer

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