1

I am pretty new to asp.net mvc. I want to get the parameter string in my url

http://localhost/Item/ItemSpec/3431?dep=62&cat=129&tab=2

How can I get the value=3431?

I tried to used HttpContext.Current.Request.QueryString["id"], but it's not work. 3431 is the id of the item that display in my page, that's why I used ["id"].

Thanks you so much.

4 Answers 4

2

The 3431 is part of the path of the request, not part of the query string. You could use HttpRequest.Path to get at the path, but MVC routing should allow you to simply write a controller method which accepts the ID as a parameter. I suggest you read up on how to configure routing. (Just searching for ASP.NET routing or MVC routing will give you lots of articles.)

Sign up to request clarification or add additional context in comments.

4 Comments

Does it can do by regular expression or something else beside routing?
@titi - sure you could, but then you would have 2 problems
@titi: Potentially - but why would you want to? Routing solves this cleanly for you - why make things more complex?
@titi: Take the time (it's small amount of time anyway) to learn Asp.net MVC properly before you give it up by vehemently stating it's too complex or it doesn't work.
2

Assuming the default route is configured in Global.asax ({controller}/{action}/{id}) you could have your controller action take an id parameter and the default model binder will automatically set its value:

public ActionResult Foo(string id)
{
    ...
}

If you want to fetch this id value from some other portion of your code that does have access to an HttpContext you need to fetch it from the RouteData:

var id = HttpContext.Request.RequestContext.RouteData["id"];

RouteData is available in all standard MVC locations. In your example you have used the static HttpContext.Current property which is something that you should never use. I suspect that you are trying to fetch this id from a portion of your code where you are not supposed to have access to the HttpContext. So you'd better fetch this id using standard techniques and then pass it as parameter to other parts of your code.

Comments

1

If Item is your controller, and ItemSpec is action, you can get the Id just by

public ActionResult ItemSpec(int id) { }

You routing have to be setup to:

context.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index"  }
);

Comments

0

If you haven't changed your routing so it's still defined as it was when you created Asp.net MVC Web project then this should be one of your controllers:

public class ItemController : ControllerBase
{
    ...

    public ActionResult ItemSpec(int id, int dep, int cat, int tab)
    {
        // implementation that uses all four values
    }

    ...
}

This is of course just one of the actions in it. There may be others as well. Most likely the Index one that's generated by default and is also used by default routing...

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.