Udemy

asp.net mvc 4 routing Urls

Sunday, August 04, 2013 2 Comments A+ a-

Hi guys,

This Saturday i got in to situation, scenario was when we pass parameters to an action when we are calling action as get not post data is passed to the action parameters but if we see our application url, the values is shown in the url as query string this way:


http://localhost:34795/TestDetails?flag=4&sub=2


which looks very awkward as its not a good approach to show database related ids in the urls, its unsafe, user just needs to play with numbers and can access the records that we dont want to be accesssible to him, so i needed a way so that my url be shown like this:



http://localhost:34795/TestDetails/4/2


or like this:

http://localhost:34795/TestDetails




When i go-ogled i came to know that we can configure the mvc application routes  in Global.asax file of our project which was very simple, but in my case it was not very simple, it was really weird and wasted my one day in achieving that i will explain why it happened. We can achieve the thing by writing this code for our particular Controller's action in Global.asax:

as in my case my Verification Controller's Index action was called and two parameters were paased to it and my url was looking like this:


http://localhost:34795/Verification?DepartmentID=3&SubDepartmentID=2 
 

and i dont't want the url to be like this, the solution is simple you have to write this code in Golobal.asax, i  am writing the code for the above controller's action:



public class MvcApplication : System.Web.HttpApplication
    {

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


            routes.MapRoute(
                "Verification",                                              // Route name
                "Verification/{DepartmentID}/{SubDepartmentID}",                           // URL with parameters
                new
                {
                    controller = "Verification",
                    action = "Index",
                    DepartmentID = UrlParameter.Optional,
                    SubDepartmentID = UrlParameter.Optional
                } // Parameter defaults
            );

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

            
        }


        protected void Application_Start()
        {

            

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            BundleTable.Bundles.RegisterTemplateBundles();
        }
    }
 
 
 


But in my case i was developing application asp.net mvc 4 and i noticed that asp.net mvc4 adds some classes to the App_Start folder automatically at project creation by defaul, i was adding routing code in the Global.asax file but in the Application_Start method this line was written which i did'nt noticed before :


protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

As you see for registering routes in Application_Start method RegisterRoutes method is called of class RouteConfig which is available in the App_Start folder of Solution, this was the issue i was  writing route code in Global.asax but in actual my that method was not calling method was called of class RouteConfig, when i realized i moved my route code to that class method, and things worked.
Coursera - Hundreds of Specializations and courses in business, computer science, data science, and more

2 comments

Write comments
August 4, 2013 at 11:14 PM delete

nice and useful information regarding Querystring info

Reply
avatar