0

i have an action which takes two parameters but when action is called, parameters are displayed in the url as query string like this:

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

I know that using custom url route i can change this to like this:

localhost:34795/Verification/3/2

but i am unable to do this i added this code to Globas.asax but no outcome till"

public class MvcApplication : System.Web.HttpApplication
{

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

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

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

        //routes.MapRoute(
        //    "Default",                                              // Route name
        //    "{controller}/{action}/{id}",                           // URL with parameters
        //    new { controller = "TestDetails", action = "GetSubDepartmentID", id = "" }  // Parameter defaults
        //);
    }


    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

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

i did this way but nothing happened, what i am doing wrong?

2
  • Can you post your view code? How are you submitting your view form? Commented Aug 3, 2013 at 13:14
  • @ckv I am redirecting from home controller using Redirect.Action Function..here is the line of code: return RedirectToAction("Index", "Verification", new RouteValueDictionary { { "DepartmentID", result.DepartmentId }, { "SubDepartmentID", result.SubdepartmentId } }); Commented Aug 5, 2013 at 2:53

2 Answers 2

2

This should do the trick:

routes.MapRoute("Blog",
    "Verification/{DepartmentID}/{SubDepartmentID}",
    new {
        controller = "Verification",
        action = "Index",
        DepartmentID = UrlParameter.Optional,
        SubDepartmentID = UrlParameter.Optional
    }
);

Then in your Index function in VerificationController:

public ActionResult Index(int DepartmentID, int SubDepartmentID)
Sign up to request clarification or add additional context in comments.

1 Comment

I have did this way but its not working url is showing same way..:(
1

I have got the solution, after thorough debugging i realized that in asp.net mvc 4 it creates some classes by default in application Shared directory and there for registering routes application was calling this method:

protected void Application_Start()
    {

        AreaRegistration.RegisterAllAreas();

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

You see here its calling RegisterRoutes method which is in RouteConfig.cs file which means in RouteConfig class its not calling the method of Global.asax, this was the issue and i got mad solving it and at last this result i got and got thing to work.

Here is my RouteConfig.cs code which solved the issue and make things work:

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

        routes.MapRoute("home", "", new { controller = "Home", action = "Index" });

        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 }
        );
    }
}

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.