16 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -75,11 +75,12 @@ public IActionResult IndexWithModel(AutoCompleteViewModel? model)
75
75
//github.com/ <param name="model">Location Search Page Elements</param>
76
76
//github.com/ <returns>Result Page</returns>
77
77
[ HttpPost ]
78
+ [ ValidateModel ]
78
79
public async Task < IActionResult > GetLocationsWithModel ( AutoCompleteViewModel model )
79
80
{
80
81
try
81
82
{
82
- if ( ! ModelState . IsValid ) return View ( "IndexWithModel" , model ) ;
83
+ // if (!ModelState.IsValid) return View("IndexWithModel", model);
83
84
84
85
var autoComplete = await CallAutoCompleteFromAPI ( model . PlaceName ) ;
85
86
Original file line number Diff line number Diff line change @@ -77,11 +77,12 @@ public IActionResult IndexWithModel(CurrentWeatherViewModel? model)
77
77
//github.com/ <param name="model">Current Weather Search Page Elements</param>
78
78
//github.com/ <returns>Result Page</returns>
79
79
[ HttpPost ]
80
+ [ ValidateModel ]
80
81
public async Task < IActionResult > GetCurrentWeatherWithModel ( CurrentWeatherViewModel model )
81
82
{
82
83
try
83
84
{
84
- if ( ! ModelState . IsValid ) return View ( "IndexWithModel" , model ) ;
85
+ // if (!ModelState.IsValid) return View("IndexWithModel", model);
85
86
86
87
var currentWeather = await GetCurrentWeatherFromAPI ( model . PlaceName , model . Unit , model . Language ) ;
87
88
Original file line number Diff line number Diff line change
1
+ using Microsoft . AspNetCore . Mvc ;
2
+ using Microsoft . AspNetCore . Mvc . Filters ;
3
+
4
+ namespace WeatherStackNetCore . Utils ;
5
+
6
+ //github.com/ <summary>
7
+ //github.com/ Model Validation Attribute Class
8
+ //github.com/ </summary>
9
+ public class ValidateModelAttribute : ActionFilterAttribute
10
+ {
11
+ //github.com/ <summary>
12
+ //github.com/ In this method, if model is not valid
13
+ //github.com/ App sends automatically bad request
14
+ //github.com/ </summary>
15
+ //github.com/ <param name="context">Context Info</param>
16
+ public override void OnActionExecuting ( ActionExecutingContext context )
17
+ {
18
+ if ( ! context . ModelState . IsValid )
19
+ context . Result = new BadRequestResult ( ) ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change 1
- 5300ac7f6b00b873d53bec5906d9fd96c024f011
1
+ 41acd01dd78252a159bc2235c4bb70e8548fb52f
0 commit comments