0

I have url like below

http://ixorra.com/products/27/13

I want 27 value in view

My URL Routing config like :

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

Any idea how to get a value of 27 in view.

6
  • According to your route, 27 is the name of your action method. Is it? It's not clear to me what you mean by "get it in the view" or what you're trying to accomplish here. Commented Dec 12, 2017 at 12:51
  • Possible duplicate of How to get part of the path with jQuery? Commented Dec 12, 2017 at 12:55
  • In URL "Products" is Action method name and 27 is my first parameter of action method and 13 is the second parameter of action method Commented Dec 12, 2017 at 12:56
  • in that case you're missing controller name Commented Dec 12, 2017 at 12:57
  • Controller name is home and "Home" is default controller in Route configuration Commented Dec 12, 2017 at 12:59

2 Answers 2

1

Based on your comments on the question above:

In URL "Products" is Action method name and 27 is my first parameter of action method and 13 is the second parameter of action method.
Controller name is home and "Home" is default controller in Route configuration.

First of all, your URL is wrong. You forgot the controller. It should be:

http://ixorra.com/home/products/27/13

Once you've corrected that, you'll just need to add another optional parameter to your route configuration. This would only change these lines:

url: "{controller}/{action}/{id}/{foo}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, foo = UrlParameter.Optional },

Naturally, you'll want to use a more meaningful name than foo. But we don't know what these values mean, so that's just a placeholder for now.

Your controller action can then accept those URL parameters:

public ActionResult Products(int id, int foo)
{
    // your action method
}

(This is where the more meaningful name becomes, well, meaningful. Whatever common generic name would make sense for that second parameter is what you'd use both in the route definition and in the method definition.)

At this point you have the value, so you can do whatever you like with it. Add it to the model that gets sent to the view, send it in the ViewBag, etc. For example:

public ActionResult Products(int id, int foo)
{
    ViewBag.foo = foo;
    return View();
}

Then in your view you can access the value from the ViewBag:

<span>The value is: @ViewBag.foo</span>
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it by split the url:

var url = $(location).attr('href');
var parts = url.split("/");
alert(parts[0]);

you can desired part usinf proper index. I hope it can help!

3 Comments

i want this value in HTML not in script portion LIKE Below @section meta_tags { if(value which are we are want) { }
I think HTML is a markup language, not a programming language. You'll need to use either JavaScript or some server side language to access and process the query string
or you can get the part you need in code behind and then pass it to view with viewmodel or viewbag or some thing like this.

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.