0

First of all, I am very new to MVC and this is my first ever project.

I am trying to achieve the custom routing URL like the following:

http://mywebsite/MDT/Index/ADC00301SB

Similar to... http://mywebsite/{Controller}/{Action}/{query}

In my RouteConfig.cs, I put the following

routes.MapRoute(
                name: "SearchComputer",
                url: "{controller}/{action}/{query}",
                defaults: new { controller = "MDT", action = "Index", query = UrlParameter.Optional }
            );

In My MDTController.cs, I have the following code

public ActionResult Index(string query)
        {
            Utils.Debug(query);
            if (string.IsNullOrEmpty(query) == false)
            {
                //Load data and return view 
                //Remove Codes for clarification
            }

            return View();
        }

But it's not working and I always get NULL value in query if I used http://mywebsite/MDT/Index/ADC00301SB

But if I used http://mywebsite/MDT?query=ADC00301SB, it's working fine and it hits the Controller Index method.

Could you please let me know how I could map the routing correctly?

TTCG
  • 8,805
  • 31
  • 93
  • 141
  • Hi if you are using MVC4 or above i would like to prefer `attribute routing` You can see the details to implement here http://grandhah.blogspot.in/2015/07/url-rewriting-in-mvc-4-attribute-routing.html – Sreerejith S S Oct 13 '15 at 09:34

3 Answers3

1

You should add your MapRoute before default MapRoute, because order in RouteCollection is important

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

            routes.MapRoute(
               name: "SearchComputer",
               url: "{controller}/{action}/{query}",
               defaults: new { controller = "MDT", action = "Index", query = UrlParameter.Optional }
           );

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


        }
Bartosz Czerwonka
  • 1,631
  • 1
  • 10
  • 11
1

One issue that I have encountered is that placing your route below the default route will cause the default to be hit, not your custom route.

So place it above the default route and it will work.

A detailed explanation from MSDN:

The order in which Route objects appear in the Routes collection is significant. Route matching is tried from the first route to the last route in the collection. When a match occurs, no more routes are evaluated. In general, add routes to the Routes property in order from the most specific route definitions to least specific ones.

Adding Routes to an MVC Application.

Ric
  • 12,855
  • 3
  • 30
  • 36
0

You can change it to

routes.MapRoute(
            name: "SearchComputer",
            url: "MDT/{action}/{query}",
            defaults: new { controller = "MDT", action = "Index", query = UrlParameter.Optional }
        );
Lewis Hai
  • 1,114
  • 10
  • 22