ASP.Net MVC - handling bad URL parameters

前端 未结 5 1743
不知归路
不知归路 2021-02-01 07:21

What\'s the best way to handle a visitor constructing their own URL and replacing what we expect to be an ID with anything they like?

For example:

ASP.Net MVC -

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 07:57

    Here is some useful infromation that might help. If you have a action method

    public ActionResult Edit(int? id)
    {}
    

    then if someone types in

    /Home/Edit/23
    

    the parameter id will be 23. however if someone types in

    /Home/Edit/Junk
    

    then id will be null which is pretty cool. I thought it would throw a cast error or something. It means that if id is not a null value then it is a valid integer and can be passed to your services etc. for db interaction.

    Hope this provides you with some info that I have found whilst testing.

提交回复
热议问题