ASP.Net MVC - handling bad URL parameters

前端 未结 5 1740
不知归路
不知归路 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 08:14

    In ASP.NET MVC, you can define a filter implementing IActionFilter interface. You will be able to decorate your action with this attribute so that it will be executed on, before or after your action.

    In your case, you will define it to be executed "before" your action. So that, you will be able to cancel it if there is an error in the passed parameters. The key benefit here that you only write the code which checking the passed paramaters once (i.e you define it in your filter) and use it wherever you want in your controller actions.

    Read more about MVC filters here: http://haacked.com/archive/2008/08/14/aspnetmvc-filters.aspx

提交回复
热议问题