2

In my MVC4 project I have an url http://domain.com/{controller}/{action}/. To determine what page user visits and and get current active menuitem I use url path like this HttpContext.Request.Url.AbsolutePath.ToLower()

However in some cases paths are /{controller}/{action}/{id} etc. which are actually /{controller}/{action}/?{id}=value.

How can I get /{controller}/{action}/ without parameters if they are overridden by routing rules?

Thank you.

2 Answers 2

3

Are you only interested in the controller and action names?

If so, use the RouteData property of the controller.

You can use it like this:

public ActionResult Index()
{
    var controller = this.RouteData.Values["controller"];
    var action = this.RouteData.Values["action"];
}
Sign up to request clarification or add additional context in comments.

2 Comments

not really, in some cases I have /Management/{controller}/{action} in some /{controller}/{action}/{id}, and in some /Management/{controller}/{action}/{id}
@DolceVita then you have to use Mukund's solution, unless you're using both the /{controller}/{action}/{id} routing and the /{controller}/{action}?{id}=value one.
0

How about adding one new route before your current route which will be like this.

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

new route will be like this -

routes.MapRoute(
                name: "**Withoutparam**",
                url: "{controller}/{action}",
                defaults: new { controller = "Home", action = "Index" }
            );

Please note that you need to add this route before your current route in RouteConfig.cs. This is because MVC starts searching routes in RouteConfig file from top to bottom. As soon as it finds any matching route it will stop searching further. So for your problem when there will be route without param it will pick "Withoutparam" route.

As I can understand from your comment below. You need url without id in every condition. If it's so, then I think you can do this: -

var url = new StringBuilder();

string[] partOfUrl = HttpContext.Request.Url.AbsolutePath.ToLower().split('/');

for(int count = 0; count< (partOfUrl.Length - 1); count++) 
{   
   url.Append(partOfLength[count] + "/") 
}

use url.ToString() as url which you want.

5 Comments

If you're not actively using the {controller}/{action}/{id} route (i.e. you pass id only through querystring) you can keep only the {controller}/{action} route.
he is using id sometime also.
Yes, but if he's passing it only as a parameter (he said "which are actually") he doesn't need the {controller}/{action}/{id} route.
I think he is saying, he don't want id in from this code . This particular line of code will return whatever url is HttpContext.Request.Url.AbsolutePath.ToLower(), right?
Exactly, I don't need parameters in QueryString, however I prefer to leave friendly urls like /{id} instead of using ?id=value... Somever MVC converts url from path to normal url, defining that /{id} corresponds to ?id=value, but where. In my Menu structure I have menus with URL columns, so I compare path and get right menu from the database. But in case if it has /{id} it does not equal to the right menu. For example, I have /clients/edit/ menu, and url is /clients/edit/2545. However I also has menu paths like /manage/users/edit, manage is just a routing path to the con/'users' with act'edit'

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.