10

I have Controller name: District and Action name: Incharges But I want the URL to be like this (action name with some paremeter)

www.example.com/district/incharges/aaa

www.example.com/district/incharges/bbb

www.example.com/district/incharges/ccc

But, while debugging teamName always return as NULL in the action parameter.

Routing

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

            routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges" }
            ); 

Controller

But, while debugging teamName always return as NULL in the action parameter.

public class DistrictController : Controller
    {     


        public ActionResult Incharges(string teamName)
        {
            InchargePresentationVM INPVM = new InchargePresentationVM();
            INPVM.InitializePath(teamName, string.Empty);
            return View("", INPVM);
        }
}

View

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>

2 Answers 2

20

specific route you have to declare the first

routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges", id = UrlParameter.Optional }

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );); 
Sign up to request clarification or add additional context in comments.

1 Comment

Remember that the parameter name is very important. In the case above the parameter name in the action must be the same as the routing rule i.e. Incharges(string id) or else change the rule to new { controller = "District", action = "Incharges", teamname = UrlParameter.Optional }
1

ASP.NET MVC DefaultModelBinder will try and do implicit type conversion of the values from your value provider , eg. form, to the action method parameters. If it tries to convert a type from the value provider to the parameter and it is not able to do it, it will assign null to the parameter.

Regarding routing, ASP.NET MVC has the concept of conversion over configuration. If you follow the conversion, then instead of configuration. You can keep your default route and always have the route you want by naming your controllers, action methods and parameter names.

With the convention over configuration you must keep the default HomeController which is the entry point of the application and then name other controllers as below. These can conform to the route names you want.

    namespace ConversionOverConfiguration
    {

      public class  DistrictController: Controller
      {
         public ActionResult Incharges(int aaa)
         {
            //You implementation here

             return View();
         }

      }

    }
The route will look as below if you have this conversion implementation
    //Controller/ActionMethod/ActionMethodParameter
    //District/Incharges/aaa

And this will give you domain URI:www.example.com/district/incharges/aaa . If action method parameter type is a string, then domain URI is:www.example.com/district/incharges/?aaa=name is a string. Then you can keep the ASP.NET MVC default routing

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

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.